Skip to content

Unaligned flexible array field and wrapped array in zend_accel_globals (static analyzer report) #17564

Closed
@Snape3058

Description

@Snape3058

Description

Struct zend_string is defined as a flexible array

php-src/Zend/zend_types.h

Lines 373 to 378 in c2fddac

struct _zend_string {
zend_refcounted_h gc;
zend_ulong h; /* hash value */
size_t len;
char val[1];
};

and used in the middle of another struct zend_accel_globals
zend_string key;
char _key[MAXPATHLEN * 8];

The offset of array header zend_string::val and the following wrapped array zend_accel_globals::_key are not aligned.

According to the output of pahole on x86_64,

struct _zend_string {
    zend_refcounted_h          gc;                   /*     0     8 */
    zend_ulong                 h;                    /*     8     8 */
    size_t                     len;                  /*    16     8 */
    char                       val[1];               /*    24     1 */

    /* size: 32, cachelines: 1, members: 4 */
    /* padding: 7 */
    /* last cacheline: 32 bytes */
};

struct _zend_accel_globals {
    /* omitted for simplicity */
    zend_string                key;                  /*   400    32 */
    char                       _key[32768];          /*   432 32768 */

    /* size: 33200, cachelines: 519, members: 25 */
    /* sum members: 33187, holes: 4, sum holes: 13 */
    /* last cacheline: 48 bytes */
};

the offset of key is 400, so its val starts from 424; whereas the offset of _key is 432.
There is a padding of 7 bytes between them.
This means that for a pointer p of type zend_accel_globals, p->key.val[1] is not p->_key[0].

When these two fields are used together, it will lead to unexpected behaviors.

Although, with a brief search with clang-query, I did not find any usages of these two fields.
I think this problem is still worth notification.

report-id: 250106-1639:7

PHP Version

latest version

Operating System

Debian 11

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions