var_unserializer.re 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Sascha Schumann <sascha@schumann.cx> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include "php.h"
  17. #include "ext/standard/php_var.h"
  18. #include "php_incomplete_class.h"
  19. #include "zend_portability.h"
  20. #include "zend_exceptions.h"
  21. /* {{{ reference-handling for unserializer: var_* */
  22. #define VAR_ENTRIES_MAX 1018 /* 1024 - offsetof(php_unserialize_data, entries) / sizeof(void*) */
  23. #define VAR_DTOR_ENTRIES_MAX 255 /* 256 - offsetof(var_dtor_entries, data) / sizeof(zval) */
  24. #define VAR_ENTRIES_DBG 0
  25. /* VAR_FLAG used in var_dtor entries to signify an entry on which
  26. * __wakeup/__unserialize should be called */
  27. #define VAR_WAKEUP_FLAG 1
  28. #define VAR_UNSERIALIZE_FLAG 2
  29. /* Each element is encoded using at least 2 characters. */
  30. #define IS_FAKE_ELEM_COUNT(num_elems, serialized_len) \
  31. ((num_elems) > (serialized_len) / 2)
  32. typedef struct {
  33. zend_long used_slots;
  34. void *next;
  35. zval *data[VAR_ENTRIES_MAX];
  36. } var_entries;
  37. typedef struct {
  38. zend_long used_slots;
  39. void *next;
  40. zval data[VAR_DTOR_ENTRIES_MAX];
  41. } var_dtor_entries;
  42. struct php_unserialize_data {
  43. var_entries *last;
  44. var_dtor_entries *first_dtor;
  45. var_dtor_entries *last_dtor;
  46. HashTable *allowed_classes;
  47. HashTable *ref_props;
  48. zend_long cur_depth;
  49. zend_long max_depth;
  50. var_entries entries;
  51. };
  52. PHPAPI php_unserialize_data_t php_var_unserialize_init(void) {
  53. php_unserialize_data_t d;
  54. /* fprintf(stderr, "UNSERIALIZE_INIT == lock: %u, level: %u\n", BG(serialize_lock), BG(unserialize).level); */
  55. if (BG(serialize_lock) || !BG(unserialize).level) {
  56. d = emalloc(sizeof(struct php_unserialize_data));
  57. d->last = &d->entries;
  58. d->first_dtor = d->last_dtor = NULL;
  59. d->allowed_classes = NULL;
  60. d->ref_props = NULL;
  61. d->cur_depth = 0;
  62. d->max_depth = BG(unserialize_max_depth);
  63. d->entries.used_slots = 0;
  64. d->entries.next = NULL;
  65. if (!BG(serialize_lock)) {
  66. BG(unserialize).data = d;
  67. BG(unserialize).level = 1;
  68. }
  69. } else {
  70. d = BG(unserialize).data;
  71. ++BG(unserialize).level;
  72. }
  73. return d;
  74. }
  75. PHPAPI void php_var_unserialize_destroy(php_unserialize_data_t d) {
  76. /* fprintf(stderr, "UNSERIALIZE_DESTROY == lock: %u, level: %u\n", BG(serialize_lock), BG(unserialize).level); */
  77. if (BG(serialize_lock) || BG(unserialize).level == 1) {
  78. var_destroy(&d);
  79. efree(d);
  80. }
  81. if (!BG(serialize_lock) && !--BG(unserialize).level) {
  82. BG(unserialize).data = NULL;
  83. }
  84. }
  85. PHPAPI HashTable *php_var_unserialize_get_allowed_classes(php_unserialize_data_t d) {
  86. return d->allowed_classes;
  87. }
  88. PHPAPI void php_var_unserialize_set_allowed_classes(php_unserialize_data_t d, HashTable *classes) {
  89. d->allowed_classes = classes;
  90. }
  91. PHPAPI void php_var_unserialize_set_max_depth(php_unserialize_data_t d, zend_long max_depth) {
  92. d->max_depth = max_depth;
  93. }
  94. PHPAPI zend_long php_var_unserialize_get_max_depth(php_unserialize_data_t d) {
  95. return d->max_depth;
  96. }
  97. PHPAPI void php_var_unserialize_set_cur_depth(php_unserialize_data_t d, zend_long cur_depth) {
  98. d->cur_depth = cur_depth;
  99. }
  100. PHPAPI zend_long php_var_unserialize_get_cur_depth(php_unserialize_data_t d) {
  101. return d->cur_depth;
  102. }
  103. static inline void var_push(php_unserialize_data_t *var_hashx, zval *rval)
  104. {
  105. var_entries *var_hash = (*var_hashx)->last;
  106. #if VAR_ENTRIES_DBG
  107. fprintf(stderr, "var_push(" ZEND_LONG_FMT "): %d\n", var_hash?var_hash->used_slots:-1L, Z_TYPE_P(rval));
  108. #endif
  109. if (var_hash->used_slots == VAR_ENTRIES_MAX) {
  110. var_hash = emalloc(sizeof(var_entries));
  111. var_hash->used_slots = 0;
  112. var_hash->next = 0;
  113. (*var_hashx)->last->next = var_hash;
  114. (*var_hashx)->last = var_hash;
  115. }
  116. var_hash->data[var_hash->used_slots++] = rval;
  117. }
  118. PHPAPI void var_push_dtor(php_unserialize_data_t *var_hashx, zval *rval)
  119. {
  120. if (Z_REFCOUNTED_P(rval)) {
  121. zval *tmp_var = var_tmp_var(var_hashx);
  122. if (!tmp_var) {
  123. return;
  124. }
  125. ZVAL_COPY(tmp_var, rval);
  126. }
  127. }
  128. static zend_never_inline void var_push_dtor_value(php_unserialize_data_t *var_hashx, zval *rval)
  129. {
  130. if (Z_REFCOUNTED_P(rval)) {
  131. zval *tmp_var = var_tmp_var(var_hashx);
  132. if (!tmp_var) {
  133. return;
  134. }
  135. ZVAL_COPY_VALUE(tmp_var, rval);
  136. }
  137. }
  138. static zend_always_inline zval *tmp_var(php_unserialize_data_t *var_hashx, zend_long num)
  139. {
  140. var_dtor_entries *var_hash;
  141. zend_long used_slots;
  142. if (!var_hashx || !*var_hashx || num < 1) {
  143. return NULL;
  144. }
  145. var_hash = (*var_hashx)->last_dtor;
  146. if (!var_hash || var_hash->used_slots + num > VAR_DTOR_ENTRIES_MAX) {
  147. var_hash = emalloc(sizeof(var_dtor_entries));
  148. var_hash->used_slots = 0;
  149. var_hash->next = 0;
  150. if (!(*var_hashx)->first_dtor) {
  151. (*var_hashx)->first_dtor = var_hash;
  152. } else {
  153. (*var_hashx)->last_dtor->next = var_hash;
  154. }
  155. (*var_hashx)->last_dtor = var_hash;
  156. }
  157. for (used_slots = var_hash->used_slots; var_hash->used_slots < used_slots + num; var_hash->used_slots++) {
  158. ZVAL_UNDEF(&var_hash->data[var_hash->used_slots]);
  159. Z_EXTRA(var_hash->data[var_hash->used_slots]) = 0;
  160. }
  161. return &var_hash->data[used_slots];
  162. }
  163. PHPAPI zval *var_tmp_var(php_unserialize_data_t *var_hashx)
  164. {
  165. return tmp_var(var_hashx, 1);
  166. }
  167. PHPAPI void var_replace(php_unserialize_data_t *var_hashx, zval *ozval, zval *nzval)
  168. {
  169. zend_long i;
  170. var_entries *var_hash = &(*var_hashx)->entries;
  171. #if VAR_ENTRIES_DBG
  172. fprintf(stderr, "var_replace(" ZEND_LONG_FMT "): %d\n", var_hash?var_hash->used_slots:-1L, Z_TYPE_P(nzval));
  173. #endif
  174. while (var_hash) {
  175. for (i = 0; i < var_hash->used_slots; i++) {
  176. if (var_hash->data[i] == ozval) {
  177. var_hash->data[i] = nzval;
  178. /* do not break here */
  179. }
  180. }
  181. var_hash = var_hash->next;
  182. }
  183. }
  184. static zval *var_access(php_unserialize_data_t *var_hashx, zend_long id)
  185. {
  186. var_entries *var_hash = &(*var_hashx)->entries;
  187. #if VAR_ENTRIES_DBG
  188. fprintf(stderr, "var_access(" ZEND_LONG_FMT "): " ZEND_LONG_FMT "\n", var_hash?var_hash->used_slots:-1L, id);
  189. #endif
  190. while (id >= VAR_ENTRIES_MAX && var_hash && var_hash->used_slots == VAR_ENTRIES_MAX) {
  191. var_hash = var_hash->next;
  192. id -= VAR_ENTRIES_MAX;
  193. }
  194. if (!var_hash) return NULL;
  195. if (id < 0 || id >= var_hash->used_slots) return NULL;
  196. return var_hash->data[id];
  197. }
  198. PHPAPI void var_destroy(php_unserialize_data_t *var_hashx)
  199. {
  200. void *next;
  201. zend_long i;
  202. var_entries *var_hash = (*var_hashx)->entries.next;
  203. var_dtor_entries *var_dtor_hash = (*var_hashx)->first_dtor;
  204. bool delayed_call_failed = 0;
  205. #if VAR_ENTRIES_DBG
  206. fprintf(stderr, "var_destroy( " ZEND_LONG_FMT ")\n", var_hash?var_hash->used_slots:-1L);
  207. #endif
  208. while (var_hash) {
  209. next = var_hash->next;
  210. efree_size(var_hash, sizeof(var_entries));
  211. var_hash = next;
  212. }
  213. while (var_dtor_hash) {
  214. for (i = 0; i < var_dtor_hash->used_slots; i++) {
  215. zval *zv = &var_dtor_hash->data[i];
  216. #if VAR_ENTRIES_DBG
  217. fprintf(stderr, "var_destroy dtor(%p, %ld)\n", &var_dtor_hash->data[i], Z_REFCOUNT_P(&var_dtor_hash->data[i]));
  218. #endif
  219. if (Z_EXTRA_P(zv) == VAR_WAKEUP_FLAG) {
  220. /* Perform delayed __wakeup calls */
  221. if (!delayed_call_failed) {
  222. zval retval;
  223. zend_fcall_info fci;
  224. zend_fcall_info_cache fci_cache;
  225. ZEND_ASSERT(Z_TYPE_P(zv) == IS_OBJECT);
  226. fci.size = sizeof(fci);
  227. fci.object = Z_OBJ_P(zv);
  228. fci.retval = &retval;
  229. fci.param_count = 0;
  230. fci.params = NULL;
  231. fci.named_params = NULL;
  232. ZVAL_UNDEF(&fci.function_name);
  233. fci_cache.function_handler = zend_hash_find_ptr(
  234. &fci.object->ce->function_table, ZSTR_KNOWN(ZEND_STR_WAKEUP));
  235. fci_cache.object = fci.object;
  236. fci_cache.called_scope = fci.object->ce;
  237. BG(serialize_lock)++;
  238. if (zend_call_function(&fci, &fci_cache) == FAILURE || Z_ISUNDEF(retval)) {
  239. delayed_call_failed = 1;
  240. GC_ADD_FLAGS(Z_OBJ_P(zv), IS_OBJ_DESTRUCTOR_CALLED);
  241. }
  242. BG(serialize_lock)--;
  243. zval_ptr_dtor(&retval);
  244. } else {
  245. GC_ADD_FLAGS(Z_OBJ_P(zv), IS_OBJ_DESTRUCTOR_CALLED);
  246. }
  247. } else if (Z_EXTRA_P(zv) == VAR_UNSERIALIZE_FLAG) {
  248. /* Perform delayed __unserialize calls */
  249. if (!delayed_call_failed) {
  250. zval param;
  251. ZVAL_COPY(&param, &var_dtor_hash->data[i + 1]);
  252. BG(serialize_lock)++;
  253. zend_call_known_instance_method_with_1_params(
  254. Z_OBJCE_P(zv)->__unserialize, Z_OBJ_P(zv), NULL, &param);
  255. if (EG(exception)) {
  256. delayed_call_failed = 1;
  257. GC_ADD_FLAGS(Z_OBJ_P(zv), IS_OBJ_DESTRUCTOR_CALLED);
  258. }
  259. BG(serialize_lock)--;
  260. zval_ptr_dtor(&param);
  261. } else {
  262. GC_ADD_FLAGS(Z_OBJ_P(zv), IS_OBJ_DESTRUCTOR_CALLED);
  263. }
  264. }
  265. i_zval_ptr_dtor(zv);
  266. }
  267. next = var_dtor_hash->next;
  268. efree_size(var_dtor_hash, sizeof(var_dtor_entries));
  269. var_dtor_hash = next;
  270. }
  271. if ((*var_hashx)->ref_props) {
  272. zend_hash_destroy((*var_hashx)->ref_props);
  273. FREE_HASHTABLE((*var_hashx)->ref_props);
  274. }
  275. }
  276. /* }}} */
  277. static zend_string *unserialize_str(const unsigned char **p, size_t len, size_t maxlen)
  278. {
  279. size_t i, j;
  280. zend_string *str = zend_string_safe_alloc(1, len, 0, 0);
  281. unsigned char *end = *(unsigned char **)p+maxlen;
  282. if (end < *p) {
  283. zend_string_efree(str);
  284. return NULL;
  285. }
  286. for (i = 0; i < len; i++) {
  287. if (*p >= end) {
  288. zend_string_efree(str);
  289. return NULL;
  290. }
  291. if (**p != '\\') {
  292. ZSTR_VAL(str)[i] = (char)**p;
  293. } else {
  294. unsigned char ch = 0;
  295. for (j = 0; j < 2; j++) {
  296. (*p)++;
  297. if (**p >= '0' && **p <= '9') {
  298. ch = (ch << 4) + (**p -'0');
  299. } else if (**p >= 'a' && **p <= 'f') {
  300. ch = (ch << 4) + (**p -'a'+10);
  301. } else if (**p >= 'A' && **p <= 'F') {
  302. ch = (ch << 4) + (**p -'A'+10);
  303. } else {
  304. zend_string_efree(str);
  305. return NULL;
  306. }
  307. }
  308. ZSTR_VAL(str)[i] = (char)ch;
  309. }
  310. (*p)++;
  311. }
  312. ZSTR_VAL(str)[i] = 0;
  313. ZSTR_LEN(str) = i;
  314. return str;
  315. }
  316. static inline int unserialize_allowed_class(
  317. zend_string *lcname, php_unserialize_data_t *var_hashx)
  318. {
  319. HashTable *classes = (*var_hashx)->allowed_classes;
  320. if(classes == NULL) {
  321. return 1;
  322. }
  323. if(!zend_hash_num_elements(classes)) {
  324. return 0;
  325. }
  326. return zend_hash_exists(classes, lcname);
  327. }
  328. #define YYFILL(n) do { } while (0)
  329. #define YYCTYPE unsigned char
  330. #define YYCURSOR cursor
  331. #define YYLIMIT limit
  332. #define YYMARKER marker
  333. /*!re2c
  334. uiv = [0-9]+;
  335. iv = [+-]? [0-9]+;
  336. nv = [+-]? ([0-9]* "." [0-9]+|[0-9]+ "." [0-9]*);
  337. nvexp = (iv | nv) [eE] iv;
  338. any = [\000-\377];
  339. object = [OC];
  340. */
  341. static inline zend_long parse_iv2(const unsigned char *p, const unsigned char **q)
  342. {
  343. zend_ulong result = 0;
  344. zend_ulong neg = 0;
  345. const unsigned char *start;
  346. if (*p == '-') {
  347. neg = 1;
  348. p++;
  349. } else if (UNEXPECTED(*p == '+')) {
  350. p++;
  351. }
  352. while (UNEXPECTED(*p == '0')) {
  353. p++;
  354. }
  355. start = p;
  356. while (*p >= '0' && *p <= '9') {
  357. result = result * 10 + ((zend_ulong)(*p) - '0');
  358. p++;
  359. }
  360. if (q) {
  361. *q = p;
  362. }
  363. /* number too long or overflow */
  364. if (UNEXPECTED(p - start > MAX_LENGTH_OF_LONG - 1)
  365. || (SIZEOF_ZEND_LONG == 4
  366. && UNEXPECTED(p - start == MAX_LENGTH_OF_LONG - 1)
  367. && UNEXPECTED(*start > '2'))
  368. || UNEXPECTED(result > ZEND_LONG_MAX + neg)) {
  369. php_error_docref(NULL, E_WARNING, "Numerical result out of range");
  370. return (!neg) ? ZEND_LONG_MAX : ZEND_LONG_MIN;
  371. }
  372. return (zend_long) ((!neg) ? result : -result);
  373. }
  374. static inline zend_long parse_iv(const unsigned char *p)
  375. {
  376. return parse_iv2(p, NULL);
  377. }
  378. /* no need to check for length - re2c already did */
  379. static inline size_t parse_uiv(const unsigned char *p)
  380. {
  381. unsigned char cursor;
  382. size_t result = 0;
  383. while (1) {
  384. cursor = *p;
  385. if (cursor >= '0' && cursor <= '9') {
  386. result = result * 10 + (size_t)(cursor - (unsigned char)'0');
  387. } else {
  388. break;
  389. }
  390. p++;
  391. }
  392. return result;
  393. }
  394. #define UNSERIALIZE_PARAMETER zval *rval, const unsigned char **p, const unsigned char *max, php_unserialize_data_t *var_hash
  395. #define UNSERIALIZE_PASSTHRU rval, p, max, var_hash
  396. static int php_var_unserialize_internal(UNSERIALIZE_PARAMETER);
  397. static zend_always_inline int process_nested_array_data(UNSERIALIZE_PARAMETER, HashTable *ht, zend_long elements)
  398. {
  399. if (var_hash) {
  400. if ((*var_hash)->max_depth > 0 && (*var_hash)->cur_depth >= (*var_hash)->max_depth) {
  401. php_error_docref(NULL, E_WARNING,
  402. "Maximum depth of " ZEND_LONG_FMT " exceeded. "
  403. "The depth limit can be changed using the max_depth unserialize() option "
  404. "or the unserialize_max_depth ini setting",
  405. (*var_hash)->max_depth);
  406. return 0;
  407. }
  408. (*var_hash)->cur_depth++;
  409. }
  410. while (elements-- > 0) {
  411. zval key, *data;
  412. zend_ulong idx;
  413. ZVAL_UNDEF(&key);
  414. if (!php_var_unserialize_internal(&key, p, max, NULL)) {
  415. zval_ptr_dtor(&key);
  416. goto failure;
  417. }
  418. if (Z_TYPE(key) == IS_LONG) {
  419. idx = Z_LVAL(key);
  420. numeric_key:
  421. data = zend_hash_index_lookup(ht, idx);
  422. if (UNEXPECTED(Z_TYPE_INFO_P(data) != IS_NULL)) {
  423. var_push_dtor_value(var_hash, data);
  424. ZVAL_NULL(data);
  425. }
  426. } else if (Z_TYPE(key) == IS_STRING) {
  427. if (UNEXPECTED(ZEND_HANDLE_NUMERIC(Z_STR(key), idx))) {
  428. zval_ptr_dtor_str(&key);
  429. goto numeric_key;
  430. }
  431. data = zend_hash_lookup(ht, Z_STR(key));
  432. if (UNEXPECTED(Z_TYPE_INFO_P(data) != IS_NULL)) {
  433. var_push_dtor_value(var_hash, data);
  434. ZVAL_NULL(data);
  435. }
  436. zval_ptr_dtor_str(&key);
  437. } else {
  438. zval_ptr_dtor(&key);
  439. goto failure;
  440. }
  441. if (!php_var_unserialize_internal(data, p, max, var_hash)) {
  442. goto failure;
  443. }
  444. if (elements && *(*p-1) != ';' && *(*p-1) != '}') {
  445. (*p)--;
  446. goto failure;
  447. }
  448. }
  449. if (var_hash) {
  450. (*var_hash)->cur_depth--;
  451. }
  452. return 1;
  453. failure:
  454. if (var_hash) {
  455. (*var_hash)->cur_depth--;
  456. }
  457. return 0;
  458. }
  459. static int is_property_visibility_changed(zend_class_entry *ce, zval *key)
  460. {
  461. if (zend_hash_num_elements(&ce->properties_info) > 0) {
  462. zend_property_info *existing_propinfo;
  463. const char *unmangled_class = NULL;
  464. const char *unmangled_prop;
  465. size_t unmangled_prop_len;
  466. if (UNEXPECTED(zend_unmangle_property_name_ex(Z_STR_P(key), &unmangled_class, &unmangled_prop, &unmangled_prop_len) == FAILURE)) {
  467. zval_ptr_dtor_str(key);
  468. return -1;
  469. }
  470. if (unmangled_class == NULL) {
  471. existing_propinfo = zend_hash_find_ptr(&ce->properties_info, Z_STR_P(key));
  472. if (existing_propinfo != NULL) {
  473. zval_ptr_dtor_str(key);
  474. ZVAL_STR_COPY(key, existing_propinfo->name);
  475. return 1;
  476. }
  477. } else {
  478. if (!strcmp(unmangled_class, "*")
  479. || !strcasecmp(unmangled_class, ZSTR_VAL(ce->name))) {
  480. existing_propinfo = zend_hash_str_find_ptr(
  481. &ce->properties_info, unmangled_prop, unmangled_prop_len);
  482. if (existing_propinfo != NULL) {
  483. zval_ptr_dtor_str(key);
  484. ZVAL_STR_COPY(key, existing_propinfo->name);
  485. return 1;
  486. }
  487. }
  488. }
  489. }
  490. return 0;
  491. }
  492. static zend_always_inline int process_nested_object_data(UNSERIALIZE_PARAMETER, HashTable *ht, zend_long elements, zend_object *obj)
  493. {
  494. if (var_hash) {
  495. if ((*var_hash)->max_depth > 0 && (*var_hash)->cur_depth >= (*var_hash)->max_depth) {
  496. php_error_docref(NULL, E_WARNING,
  497. "Maximum depth of " ZEND_LONG_FMT " exceeded. "
  498. "The depth limit can be changed using the max_depth unserialize() option "
  499. "or the unserialize_max_depth ini setting",
  500. (*var_hash)->max_depth);
  501. return 0;
  502. }
  503. (*var_hash)->cur_depth++;
  504. }
  505. while (elements-- > 0) {
  506. zval key, *data;
  507. zend_property_info *info = NULL;
  508. ZVAL_UNDEF(&key);
  509. if (!php_var_unserialize_internal(&key, p, max, NULL)) {
  510. zval_ptr_dtor(&key);
  511. goto failure;
  512. }
  513. if (EXPECTED(Z_TYPE(key) == IS_STRING)) {
  514. string_key:
  515. data = zend_hash_find(ht, Z_STR(key));
  516. if (data != NULL) {
  517. if (Z_TYPE_P(data) == IS_INDIRECT) {
  518. declared_property:
  519. /* This is a property with a declaration */
  520. data = Z_INDIRECT_P(data);
  521. info = zend_get_typed_property_info_for_slot(obj, data);
  522. if (info) {
  523. if (Z_ISREF_P(data)) {
  524. /* If the value is overwritten, remove old type source from ref. */
  525. ZEND_REF_DEL_TYPE_SOURCE(Z_REF_P(data), info);
  526. }
  527. if ((*var_hash)->ref_props) {
  528. /* Remove old entry from ref_props table, if it exists. */
  529. zend_hash_index_del(
  530. (*var_hash)->ref_props, (zend_uintptr_t) data);
  531. }
  532. }
  533. /* We may override default property value, but they are usually immutable */
  534. if (Z_REFCOUNTED_P(data)) {
  535. var_push_dtor_value(var_hash, data);
  536. }
  537. ZVAL_NULL(data);
  538. } else {
  539. /* Unusual override of dynamic property */
  540. int ret = is_property_visibility_changed(obj->ce, &key);
  541. if (ret > 0) {
  542. goto second_try;
  543. } else if (!ret) {
  544. var_push_dtor_value(var_hash, data);
  545. ZVAL_NULL(data);
  546. } else if (ret < 0) {
  547. goto failure;
  548. }
  549. }
  550. } else {
  551. int ret = is_property_visibility_changed(obj->ce, &key);
  552. if (EXPECTED(!ret)) {
  553. data = zend_hash_add_new(ht, Z_STR(key), &EG(uninitialized_zval));
  554. } else if (ret < 0) {
  555. goto failure;
  556. } else {
  557. second_try:
  558. data = zend_hash_lookup(ht, Z_STR(key));
  559. if (Z_TYPE_P(data) == IS_INDIRECT) {
  560. goto declared_property;
  561. } else if (UNEXPECTED(Z_TYPE_INFO_P(data) != IS_NULL)) {
  562. var_push_dtor_value(var_hash, data);
  563. ZVAL_NULL(data);
  564. }
  565. }
  566. }
  567. zval_ptr_dtor_str(&key);
  568. } else if (Z_TYPE(key) == IS_LONG) {
  569. /* object properties should include no integers */
  570. convert_to_string(&key);
  571. goto string_key;
  572. } else {
  573. zval_ptr_dtor(&key);
  574. goto failure;
  575. }
  576. if (!php_var_unserialize_internal(data, p, max, var_hash)) {
  577. if (info && Z_ISREF_P(data)) {
  578. /* Add type source even if we failed to unserialize.
  579. * The data is still stored in the property. */
  580. ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(data), info);
  581. }
  582. goto failure;
  583. }
  584. if (UNEXPECTED(info)) {
  585. if (!zend_verify_prop_assignable_by_ref(info, data, /* strict */ 1)) {
  586. zval_ptr_dtor(data);
  587. ZVAL_UNDEF(data);
  588. goto failure;
  589. }
  590. if (Z_ISREF_P(data)) {
  591. ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(data), info);
  592. } else {
  593. /* Remember to which property this slot belongs, so we can add a
  594. * type source if it is turned into a reference lateron. */
  595. if (!(*var_hash)->ref_props) {
  596. (*var_hash)->ref_props = emalloc(sizeof(HashTable));
  597. zend_hash_init((*var_hash)->ref_props, 8, NULL, NULL, 0);
  598. }
  599. zend_hash_index_update_ptr(
  600. (*var_hash)->ref_props, (zend_uintptr_t) data, info);
  601. }
  602. }
  603. if (elements && *(*p-1) != ';' && *(*p-1) != '}') {
  604. (*p)--;
  605. goto failure;
  606. }
  607. }
  608. if (var_hash) {
  609. (*var_hash)->cur_depth--;
  610. }
  611. return 1;
  612. failure:
  613. if (var_hash) {
  614. (*var_hash)->cur_depth--;
  615. }
  616. return 0;
  617. }
  618. static inline int finish_nested_data(UNSERIALIZE_PARAMETER)
  619. {
  620. if (*p >= max || **p != '}') {
  621. return 0;
  622. }
  623. (*p)++;
  624. return 1;
  625. }
  626. static inline int object_custom(UNSERIALIZE_PARAMETER, zend_class_entry *ce)
  627. {
  628. zend_long datalen;
  629. datalen = parse_iv2((*p) + 2, p);
  630. (*p) += 2;
  631. if (datalen < 0 || (max - (*p)) <= datalen) {
  632. zend_error(E_WARNING, "Insufficient data for unserializing - " ZEND_LONG_FMT " required, " ZEND_LONG_FMT " present", datalen, (zend_long)(max - (*p)));
  633. return 0;
  634. }
  635. /* Check that '}' is present before calling ce->unserialize() to mitigate issues
  636. * with unserialize reading past the end of the passed buffer if the string is not
  637. * appropriately terminated (usually NUL terminated, but '}' is also sufficient.) */
  638. if ((*p)[datalen] != '}') {
  639. return 0;
  640. }
  641. if (ce->unserialize == NULL) {
  642. zend_error(E_WARNING, "Class %s has no unserializer", ZSTR_VAL(ce->name));
  643. object_init_ex(rval, ce);
  644. } else if (ce->unserialize(rval, ce, (const unsigned char*)*p, datalen, (zend_unserialize_data *)var_hash) != SUCCESS) {
  645. return 0;
  646. }
  647. (*p) += datalen + 1; /* +1 for '}' */
  648. return 1;
  649. }
  650. #ifdef PHP_WIN32
  651. # pragma optimize("", off)
  652. #endif
  653. static inline int object_common(UNSERIALIZE_PARAMETER, zend_long elements, bool has_unserialize)
  654. {
  655. HashTable *ht;
  656. bool has_wakeup;
  657. if (has_unserialize) {
  658. zval ary, *tmp;
  659. if (elements >= HT_MAX_SIZE) {
  660. return 0;
  661. }
  662. array_init_size(&ary, elements);
  663. /* Avoid reallocation due to packed -> mixed conversion. */
  664. zend_hash_real_init_mixed(Z_ARRVAL(ary));
  665. if (!process_nested_array_data(UNSERIALIZE_PASSTHRU, Z_ARRVAL(ary), elements)) {
  666. ZVAL_DEREF(rval);
  667. GC_ADD_FLAGS(Z_OBJ_P(rval), IS_OBJ_DESTRUCTOR_CALLED);
  668. zval_ptr_dtor(&ary);
  669. return 0;
  670. }
  671. /* Delay __unserialize() call until end of serialization. We use two slots here to
  672. * store both the object and the unserialized data array. */
  673. ZVAL_DEREF(rval);
  674. tmp = tmp_var(var_hash, 2);
  675. ZVAL_COPY(tmp, rval);
  676. Z_EXTRA_P(tmp) = VAR_UNSERIALIZE_FLAG;
  677. tmp++;
  678. ZVAL_COPY_VALUE(tmp, &ary);
  679. return finish_nested_data(UNSERIALIZE_PASSTHRU);
  680. }
  681. has_wakeup = Z_OBJCE_P(rval) != PHP_IC_ENTRY
  682. && zend_hash_exists(&Z_OBJCE_P(rval)->function_table, ZSTR_KNOWN(ZEND_STR_WAKEUP));
  683. ht = Z_OBJPROP_P(rval);
  684. if (elements >= (zend_long)(HT_MAX_SIZE - zend_hash_num_elements(ht))) {
  685. return 0;
  686. }
  687. zend_hash_extend(ht, zend_hash_num_elements(ht) + elements, HT_FLAGS(ht) & HASH_FLAG_PACKED);
  688. if (!process_nested_object_data(UNSERIALIZE_PASSTHRU, ht, elements, Z_OBJ_P(rval))) {
  689. if (has_wakeup) {
  690. ZVAL_DEREF(rval);
  691. GC_ADD_FLAGS(Z_OBJ_P(rval), IS_OBJ_DESTRUCTOR_CALLED);
  692. }
  693. return 0;
  694. }
  695. ZVAL_DEREF(rval);
  696. if (has_wakeup) {
  697. /* Delay __wakeup call until end of serialization */
  698. zval *wakeup_var = var_tmp_var(var_hash);
  699. ZVAL_COPY(wakeup_var, rval);
  700. Z_EXTRA_P(wakeup_var) = VAR_WAKEUP_FLAG;
  701. }
  702. return finish_nested_data(UNSERIALIZE_PASSTHRU);
  703. }
  704. #ifdef PHP_WIN32
  705. # pragma optimize("", on)
  706. #endif
  707. PHPAPI int php_var_unserialize(UNSERIALIZE_PARAMETER)
  708. {
  709. var_entries *orig_var_entries = (*var_hash)->last;
  710. zend_long orig_used_slots = orig_var_entries ? orig_var_entries->used_slots : 0;
  711. int result;
  712. result = php_var_unserialize_internal(UNSERIALIZE_PASSTHRU);
  713. if (!result) {
  714. /* If the unserialization failed, mark all elements that have been added to var_hash
  715. * as NULL. This will forbid their use by other unserialize() calls in the same
  716. * unserialization context. */
  717. var_entries *e = orig_var_entries;
  718. zend_long s = orig_used_slots;
  719. while (e) {
  720. for (; s < e->used_slots; s++) {
  721. e->data[s] = NULL;
  722. }
  723. e = e->next;
  724. s = 0;
  725. }
  726. }
  727. return result;
  728. }
  729. static int php_var_unserialize_internal(UNSERIALIZE_PARAMETER)
  730. {
  731. const unsigned char *cursor, *limit, *marker, *start;
  732. zval *rval_ref;
  733. limit = max;
  734. cursor = *p;
  735. if (YYCURSOR >= YYLIMIT) {
  736. return 0;
  737. }
  738. if (var_hash && (*p)[0] != 'R') {
  739. var_push(var_hash, rval);
  740. }
  741. start = cursor;
  742. /*!re2c
  743. "R:" uiv ";" {
  744. zend_long id;
  745. *p = YYCURSOR;
  746. if (!var_hash) return 0;
  747. id = parse_uiv(start + 2) - 1;
  748. if (id == -1 || (rval_ref = var_access(var_hash, id)) == NULL) {
  749. return 0;
  750. }
  751. if (rval_ref == rval || (Z_ISREF_P(rval_ref) && Z_REFVAL_P(rval_ref) == rval)) {
  752. return 0;
  753. }
  754. if (!Z_ISREF_P(rval_ref)) {
  755. zend_property_info *info = NULL;
  756. if ((*var_hash)->ref_props) {
  757. info = zend_hash_index_find_ptr((*var_hash)->ref_props, (zend_uintptr_t) rval_ref);
  758. }
  759. ZVAL_NEW_REF(rval_ref, rval_ref);
  760. if (info) {
  761. ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(rval_ref), info);
  762. }
  763. }
  764. ZVAL_COPY(rval, rval_ref);
  765. return 1;
  766. }
  767. "r:" uiv ";" {
  768. zend_long id;
  769. *p = YYCURSOR;
  770. if (!var_hash) return 0;
  771. id = parse_uiv(start + 2) - 1;
  772. if (id == -1 || (rval_ref = var_access(var_hash, id)) == NULL) {
  773. return 0;
  774. }
  775. if (rval_ref == rval) {
  776. return 0;
  777. }
  778. ZVAL_DEREF(rval_ref);
  779. if (Z_TYPE_P(rval_ref) != IS_OBJECT) {
  780. return 0;
  781. }
  782. ZVAL_COPY(rval, rval_ref);
  783. return 1;
  784. }
  785. "N;" {
  786. *p = YYCURSOR;
  787. ZVAL_NULL(rval);
  788. return 1;
  789. }
  790. "b:0;" {
  791. *p = YYCURSOR;
  792. ZVAL_FALSE(rval);
  793. return 1;
  794. }
  795. "b:1;" {
  796. *p = YYCURSOR;
  797. ZVAL_TRUE(rval);
  798. return 1;
  799. }
  800. "i:" iv ";" {
  801. #if SIZEOF_ZEND_LONG == 4
  802. int digits = YYCURSOR - start - 3;
  803. if (start[2] == '-' || start[2] == '+') {
  804. digits--;
  805. }
  806. /* Use double for large zend_long values that were serialized on a 64-bit system */
  807. if (digits >= MAX_LENGTH_OF_LONG - 1) {
  808. if (digits == MAX_LENGTH_OF_LONG - 1) {
  809. int cmp = strncmp((char*)YYCURSOR - MAX_LENGTH_OF_LONG, long_min_digits, MAX_LENGTH_OF_LONG - 1);
  810. if (!(cmp < 0 || (cmp == 0 && start[2] == '-'))) {
  811. goto use_double;
  812. }
  813. } else {
  814. goto use_double;
  815. }
  816. }
  817. #endif
  818. *p = YYCURSOR;
  819. ZVAL_LONG(rval, parse_iv(start + 2));
  820. return 1;
  821. }
  822. "d:" ("NAN" | "-"? "INF") ";" {
  823. *p = YYCURSOR;
  824. if (!strncmp((char*)start + 2, "NAN", 3)) {
  825. ZVAL_DOUBLE(rval, ZEND_NAN);
  826. } else if (!strncmp((char*)start + 2, "INF", 3)) {
  827. ZVAL_DOUBLE(rval, ZEND_INFINITY);
  828. } else if (!strncmp((char*)start + 2, "-INF", 4)) {
  829. ZVAL_DOUBLE(rval, -ZEND_INFINITY);
  830. } else {
  831. ZVAL_NULL(rval);
  832. }
  833. return 1;
  834. }
  835. "d:" (iv | nv | nvexp) ";" {
  836. #if SIZEOF_ZEND_LONG == 4
  837. use_double:
  838. #endif
  839. *p = YYCURSOR;
  840. ZVAL_DOUBLE(rval, zend_strtod((const char *)start + 2, NULL));
  841. return 1;
  842. }
  843. "s:" uiv ":" ["] {
  844. size_t len, maxlen;
  845. char *str;
  846. len = parse_uiv(start + 2);
  847. maxlen = max - YYCURSOR;
  848. if (maxlen < len) {
  849. *p = start + 2;
  850. return 0;
  851. }
  852. str = (char*)YYCURSOR;
  853. YYCURSOR += len;
  854. if (*(YYCURSOR) != '"') {
  855. *p = YYCURSOR;
  856. return 0;
  857. }
  858. if (*(YYCURSOR + 1) != ';') {
  859. *p = YYCURSOR + 1;
  860. return 0;
  861. }
  862. YYCURSOR += 2;
  863. *p = YYCURSOR;
  864. if (!var_hash) {
  865. /* Array or object key unserialization */
  866. ZVAL_STR(rval, zend_string_init_existing_interned(str, len, 0));
  867. } else {
  868. ZVAL_STRINGL_FAST(rval, str, len);
  869. }
  870. return 1;
  871. }
  872. "S:" uiv ":" ["] {
  873. size_t len, maxlen;
  874. zend_string *str;
  875. len = parse_uiv(start + 2);
  876. maxlen = max - YYCURSOR;
  877. if (maxlen < len) {
  878. *p = start + 2;
  879. return 0;
  880. }
  881. if ((str = unserialize_str(&YYCURSOR, len, maxlen)) == NULL) {
  882. return 0;
  883. }
  884. if (*(YYCURSOR) != '"') {
  885. zend_string_efree(str);
  886. *p = YYCURSOR;
  887. return 0;
  888. }
  889. if (*(YYCURSOR + 1) != ';') {
  890. efree(str);
  891. *p = YYCURSOR + 1;
  892. return 0;
  893. }
  894. YYCURSOR += 2;
  895. *p = YYCURSOR;
  896. ZVAL_STR(rval, str);
  897. return 1;
  898. }
  899. "a:" uiv ":" "{" {
  900. zend_long elements = parse_iv(start + 2);
  901. /* use iv() not uiv() in order to check data range */
  902. *p = YYCURSOR;
  903. if (!var_hash) return 0;
  904. if (elements < 0 || elements >= HT_MAX_SIZE || IS_FAKE_ELEM_COUNT(elements, max - YYCURSOR)) {
  905. return 0;
  906. }
  907. if (elements) {
  908. array_init_size(rval, elements);
  909. /* we can't convert from packed to hash during unserialization, because
  910. reference to some zvals might be kept in var_hash (to support references) */
  911. zend_hash_real_init_mixed(Z_ARRVAL_P(rval));
  912. } else {
  913. ZVAL_EMPTY_ARRAY(rval);
  914. return finish_nested_data(UNSERIALIZE_PASSTHRU);
  915. }
  916. /* The array may contain references to itself, in which case we'll be modifying an
  917. * rc>1 array. This is okay, since the array is, ostensibly, only visible to
  918. * unserialize (in practice unserialization handlers also see it). Ideally we should
  919. * prohibit "r:" references to non-objects, as we only generate them for objects. */
  920. HT_ALLOW_COW_VIOLATION(Z_ARRVAL_P(rval));
  921. if (!process_nested_array_data(UNSERIALIZE_PASSTHRU, Z_ARRVAL_P(rval), elements)) {
  922. return 0;
  923. }
  924. return finish_nested_data(UNSERIALIZE_PASSTHRU);
  925. }
  926. object ":" uiv ":" ["] {
  927. size_t len, maxlen;
  928. zend_long elements;
  929. char *str;
  930. zend_string *class_name;
  931. zend_class_entry *ce;
  932. bool incomplete_class = 0;
  933. bool custom_object = 0;
  934. bool has_unserialize = 0;
  935. zval user_func;
  936. zval retval;
  937. zval args[1];
  938. if (!var_hash) return 0;
  939. if (*start == 'C') {
  940. custom_object = 1;
  941. }
  942. len = parse_uiv(start + 2);
  943. maxlen = max - YYCURSOR;
  944. if (maxlen < len || len == 0) {
  945. *p = start + 2;
  946. return 0;
  947. }
  948. str = (char*)YYCURSOR;
  949. YYCURSOR += len;
  950. if (*(YYCURSOR) != '"') {
  951. *p = YYCURSOR;
  952. return 0;
  953. }
  954. if (*(YYCURSOR+1) != ':') {
  955. *p = YYCURSOR+1;
  956. return 0;
  957. }
  958. if (len == 0) {
  959. /* empty class names are not allowed */
  960. return 0;
  961. }
  962. if (str[0] == '\000') {
  963. /* runtime definition keys are not allowed */
  964. return 0;
  965. }
  966. if (str[0] == '\\') {
  967. /* class name can't start from namespace separator */
  968. return 0;
  969. }
  970. class_name = zend_string_init_interned(str, len, 0);
  971. do {
  972. zend_string *lc_name;
  973. if (!(*var_hash)->allowed_classes && ZSTR_HAS_CE_CACHE(class_name)) {
  974. ce = ZSTR_GET_CE_CACHE(class_name);
  975. if (ce) {
  976. break;
  977. }
  978. }
  979. lc_name = zend_string_tolower(class_name);
  980. if(!unserialize_allowed_class(lc_name, var_hash)) {
  981. zend_string_release_ex(lc_name, 0);
  982. if (!zend_is_valid_class_name(class_name)) {
  983. zend_string_release_ex(class_name, 0);
  984. return 0;
  985. }
  986. incomplete_class = 1;
  987. ce = PHP_IC_ENTRY;
  988. break;
  989. }
  990. if ((*var_hash)->allowed_classes && ZSTR_HAS_CE_CACHE(class_name)) {
  991. ce = ZSTR_GET_CE_CACHE(class_name);
  992. if (ce) {
  993. zend_string_release_ex(lc_name, 0);
  994. break;
  995. }
  996. }
  997. ce = zend_hash_find_ptr(EG(class_table), lc_name);
  998. if (ce
  999. && (ce->ce_flags & ZEND_ACC_LINKED)
  1000. && !(ce->ce_flags & ZEND_ACC_ANON_CLASS)) {
  1001. zend_string_release_ex(lc_name, 0);
  1002. break;
  1003. }
  1004. if (!ZSTR_HAS_CE_CACHE(class_name) && !zend_is_valid_class_name(class_name)) {
  1005. zend_string_release_ex(lc_name, 0);
  1006. zend_string_release_ex(class_name, 0);
  1007. return 0;
  1008. }
  1009. /* Try to find class directly */
  1010. BG(serialize_lock)++;
  1011. ce = zend_lookup_class_ex(class_name, lc_name, 0);
  1012. zend_string_release_ex(lc_name, 0);
  1013. if (ce) {
  1014. BG(serialize_lock)--;
  1015. if (EG(exception)) {
  1016. zend_string_release_ex(class_name, 0);
  1017. return 0;
  1018. }
  1019. break;
  1020. }
  1021. BG(serialize_lock)--;
  1022. if (EG(exception)) {
  1023. zend_string_release_ex(class_name, 0);
  1024. return 0;
  1025. }
  1026. /* Check for unserialize callback */
  1027. if ((PG(unserialize_callback_func) == NULL) || (PG(unserialize_callback_func)[0] == '\0')) {
  1028. incomplete_class = 1;
  1029. ce = PHP_IC_ENTRY;
  1030. break;
  1031. }
  1032. /* Call unserialize callback */
  1033. ZVAL_STRING(&user_func, PG(unserialize_callback_func));
  1034. ZVAL_STR_COPY(&args[0], class_name);
  1035. BG(serialize_lock)++;
  1036. if (call_user_function(NULL, NULL, &user_func, &retval, 1, args) != SUCCESS) {
  1037. BG(serialize_lock)--;
  1038. if (EG(exception)) {
  1039. zend_string_release_ex(class_name, 0);
  1040. zval_ptr_dtor(&user_func);
  1041. zval_ptr_dtor(&args[0]);
  1042. return 0;
  1043. }
  1044. php_error_docref(NULL, E_WARNING, "defined (%s) but not found", Z_STRVAL(user_func));
  1045. incomplete_class = 1;
  1046. ce = PHP_IC_ENTRY;
  1047. zval_ptr_dtor(&user_func);
  1048. zval_ptr_dtor(&args[0]);
  1049. break;
  1050. }
  1051. BG(serialize_lock)--;
  1052. zval_ptr_dtor(&retval);
  1053. if (EG(exception)) {
  1054. zend_string_release_ex(class_name, 0);
  1055. zval_ptr_dtor(&user_func);
  1056. zval_ptr_dtor(&args[0]);
  1057. return 0;
  1058. }
  1059. /* The callback function may have defined the class */
  1060. BG(serialize_lock)++;
  1061. if ((ce = zend_lookup_class(class_name)) == NULL) {
  1062. php_error_docref(NULL, E_WARNING, "Function %s() hasn't defined the class it was called for", Z_STRVAL(user_func));
  1063. incomplete_class = 1;
  1064. ce = PHP_IC_ENTRY;
  1065. }
  1066. BG(serialize_lock)--;
  1067. zval_ptr_dtor(&user_func);
  1068. zval_ptr_dtor(&args[0]);
  1069. } while (0);
  1070. *p = YYCURSOR;
  1071. if (ce->ce_flags & ZEND_ACC_NOT_SERIALIZABLE) {
  1072. zend_throw_exception_ex(NULL, 0, "Unserialization of '%s' is not allowed",
  1073. ZSTR_VAL(ce->name));
  1074. zend_string_release_ex(class_name, 0);
  1075. return 0;
  1076. }
  1077. if (custom_object) {
  1078. int ret;
  1079. ret = object_custom(UNSERIALIZE_PASSTHRU, ce);
  1080. if (ret && incomplete_class) {
  1081. php_store_class_name(rval, class_name);
  1082. }
  1083. zend_string_release_ex(class_name, 0);
  1084. return ret;
  1085. }
  1086. if (*p >= max - 2) {
  1087. zend_error(E_WARNING, "Bad unserialize data");
  1088. zend_string_release_ex(class_name, 0);
  1089. return 0;
  1090. }
  1091. elements = parse_iv2(*p + 2, p);
  1092. if (elements < 0 || IS_FAKE_ELEM_COUNT(elements, max - YYCURSOR)) {
  1093. zend_string_release_ex(class_name, 0);
  1094. return 0;
  1095. }
  1096. *p += 2;
  1097. has_unserialize = !incomplete_class && ce->__unserialize;
  1098. /* If this class implements Serializable, it should not land here but in object_custom().
  1099. * The passed string obviously doesn't descend from the regular serializer. However, if
  1100. * there is both Serializable::unserialize() and __unserialize(), then both may be used,
  1101. * depending on the serialization format. */
  1102. if (ce->serialize != NULL && !has_unserialize) {
  1103. zend_error(E_WARNING, "Erroneous data format for unserializing '%s'", ZSTR_VAL(ce->name));
  1104. zend_string_release_ex(class_name, 0);
  1105. return 0;
  1106. }
  1107. if (object_init_ex(rval, ce) == FAILURE) {
  1108. zend_string_release_ex(class_name, 0);
  1109. return 0;
  1110. }
  1111. if (incomplete_class) {
  1112. php_store_class_name(rval, class_name);
  1113. }
  1114. zend_string_release_ex(class_name, 0);
  1115. return object_common(UNSERIALIZE_PASSTHRU, elements, has_unserialize);
  1116. }
  1117. "E:" uiv ":" ["] {
  1118. if (!var_hash) return 0;
  1119. size_t len = parse_uiv(start + 2);
  1120. size_t maxlen = max - YYCURSOR;
  1121. if (maxlen < len || len == 0) {
  1122. *p = start + 2;
  1123. return 0;
  1124. }
  1125. char *str = (char *) YYCURSOR;
  1126. YYCURSOR += len;
  1127. if (*(YYCURSOR) != '"') {
  1128. *p = YYCURSOR;
  1129. return 0;
  1130. }
  1131. if (*(YYCURSOR+1) != ';') {
  1132. *p = YYCURSOR+1;
  1133. return 0;
  1134. }
  1135. char *colon_ptr = memchr(str, ':', len);
  1136. if (colon_ptr == NULL) {
  1137. php_error_docref(NULL, E_WARNING, "Invalid enum name '%.*s' (missing colon)", (int) len, str);
  1138. return 0;
  1139. }
  1140. size_t colon_pos = colon_ptr - str;
  1141. zend_string *enum_name = zend_string_init(str, colon_pos, 0);
  1142. zend_string *case_name = zend_string_init(&str[colon_pos + 1], len - colon_pos - 1, 0);
  1143. if (!zend_is_valid_class_name(enum_name)) {
  1144. goto fail;
  1145. }
  1146. zend_class_entry *ce = zend_lookup_class(enum_name);
  1147. if (!ce) {
  1148. php_error_docref(NULL, E_WARNING, "Class '%s' not found", ZSTR_VAL(enum_name));
  1149. goto fail;
  1150. }
  1151. if (!(ce->ce_flags & ZEND_ACC_ENUM)) {
  1152. php_error_docref(NULL, E_WARNING, "Class '%s' is not an enum", ZSTR_VAL(enum_name));
  1153. goto fail;
  1154. }
  1155. YYCURSOR += 2;
  1156. *p = YYCURSOR;
  1157. zend_class_constant *c = zend_hash_find_ptr(CE_CONSTANTS_TABLE(ce), case_name);
  1158. if (!c) {
  1159. php_error_docref(NULL, E_WARNING, "Undefined constant %s::%s", ZSTR_VAL(enum_name), ZSTR_VAL(case_name));
  1160. goto fail;
  1161. }
  1162. if (!(ZEND_CLASS_CONST_FLAGS(c) & ZEND_CLASS_CONST_IS_CASE)) {
  1163. php_error_docref(NULL, E_WARNING, "%s::%s is not an enum case", ZSTR_VAL(enum_name), ZSTR_VAL(case_name));
  1164. goto fail;
  1165. }
  1166. zend_string_release_ex(enum_name, 0);
  1167. zend_string_release_ex(case_name, 0);
  1168. zval *value = &c->value;
  1169. if (Z_TYPE_P(value) == IS_CONSTANT_AST) {
  1170. if (zval_update_constant_ex(value, c->ce) == FAILURE) {
  1171. return 0;
  1172. }
  1173. }
  1174. ZEND_ASSERT(Z_TYPE_P(value) == IS_OBJECT);
  1175. ZVAL_COPY(rval, value);
  1176. return 1;
  1177. fail:
  1178. zend_string_release_ex(enum_name, 0);
  1179. zend_string_release_ex(case_name, 0);
  1180. return 0;
  1181. }
  1182. "}" {
  1183. /* this is the case where we have less data than planned */
  1184. php_error_docref(NULL, E_NOTICE, "Unexpected end of serialized data");
  1185. return 0; /* not sure if it should be 0 or 1 here? */
  1186. }
  1187. any { return 0; }
  1188. */
  1189. return 0;
  1190. }