Skip to content

pkcs11: bound the config file index in pkcs11_config_load_objects() - #428

Open
learnrahulrai-ui wants to merge 1 commit into
MicrochipTech:mainfrom
learnrahulrai-ui:pkcs11-config-bounds
Open

pkcs11: bound the config file index in pkcs11_config_load_objects()#428
learnrahulrai-ui wants to merge 1 commit into
MicrochipTech:mainfrom
learnrahulrai-ui:pkcs11-config-bounds

Conversation

@learnrahulrai-ui

@learnrahulrai-ui learnrahulrai-ui commented Aug 15, 2026

Copy link
Copy Markdown

updateConfFileData[MAX_CONF_FILES] is indexed by i, which nothing bounds — MAX_CONF_FILES appears only in its #define and that declaration, never in a comparison.

Under AddressSanitizer at d49c7d5, varying only the number of .conf files in the filestore:

15 files   ok
16 files   stack-buffer-overflow READ  of size 8 at :1114
17 files   stack-buffer-overflow WRITE of size 8 at :1083

Two defects, not one. At 17 the store writes past the end. At 16 nothing is written out of bounds — the array is merely full, so the NULL that the parse walk and the free walk rely on as a terminator no longer exists. Bounding the store alone leaves 16 broken, so both walks are bounded by totalConfFileCount too.

16 is a normal configuration, not a crafted one: a slot with N objects is 1 + N files (0.conf plus 0.<handle>.conf), so the default PKCS11_MAX_OBJECTS_ALLOWED=16 needs 17.

i++ also moves inside the allocation-success branch, so i and totalConfFileCount cannot diverge; on a failed malloc the old code left a NULL hole that truncates one consumer loop and is dereferenced by the other.

To reproduce: put 0.conf and 0.2.conf0.17.conf in a filestore and call C_Initialize() on a build with -fsanitize=address.

Open question: MAX_CONF_FILES is PKCS11_MAX_SLOTS_ALLOWED * PKCS11_MAX_OBJECTS_ALLOWED, counting neither the per-slot base file nor the NULL these loops need. Resizing it changes stack usage on embedded targets, so this patch only bounds the index — happy to follow up with the resize if you would prefer that.

Checklist

updateConfFileData[] holds MAX_CONF_FILES entries but the index i is never
compared against it. A filestore with 17 .conf files stores past the end at
:1083; with 16 the array is full, leaving no NULL for the walks at :1114 and
:1365, which then read past it.

Bound the store and report CKR_HOST_MEMORY rather than dropping files, since
which files got dropped would depend on readdir() order. Bound both walks by
totalConfFileCount, and increment i only when the allocation succeeded so the
two cannot disagree.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant