Repository navigation
Fixed out-of-bounds read of extended attribute values in filestat() - #6429
Open
Dhanu-dynamic wants to merge 1 commit into
Open
Dhanu-dynamic wants to merge 1 commit into
Dhanu-dynamic wants to merge 1 commit into
Conversation
The "xattr" branch of filestat() reads each extended attribute value
into an uninitialized stack buffer and then uses it as a C string:
char data[CF_BUFSIZE];
int datasize = lgetxattr(path, current, data, sizeof(data));
...
if (!StringIsPrintable(data))
...
BufferPrintf(printattr, "%s=%s", current, data);
lgetxattr() returns raw bytes and never adds a terminating NUL. An
unprivileged user can set user.* attributes on files they own, so when
a policy run as root calls filestat() on such a file, a printable value
of exactly 4096 bytes makes StringIsPrintable() read past the end of
data. Shorter values are followed by whatever stale stack bytes come
before the next NUL, which either makes a valid attribute get skipped
as non-printable or copies stack contents into the returned string.
Read at most sizeof(data) - 1 bytes and terminate the value at
datasize. CopyFileExtendedAttributesDisk() in files_copy.c, the other
lgetxattr() caller, handles values by length and is not affected.
Changelog: Title
Signed-off-by: Dhanraj E <dhanraj@labs.digiscrypt.com>
|
Thank you for submitting a PR! Maybe @craigcomstock can review this? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Repro: as an unprivileged user,
setfattr -n user.x -v "$(head -c 4096 /dev/zero | tr '\0' A)" ~/f, then have a root policy evaluatefilestat("/home/user/f", "xattr"). Under ASan this is a stack-buffer-overflow read inStringIsPrintable(). A short value such asuser.x=abcis followed by stale stack bytes, so it is either dropped as non-printable or returned with stack contents appended.Cause:
lgetxattr()fillsdata[CF_BUFSIZE]with raw bytes and never NUL-terminates, butFnCallFileStatDetails()then usesdataas a C string.Fix: read at most
sizeof(data) - 1bytes and terminate atdatasize.CopyFileExtendedAttributesDisk()infiles_copy.chandles values by length and is not affected.I checked this by running the same loop against real extended attributes under ASan: the over-read before the change,
user.x=abcreturned correctly after it. The unit test suite still passes. I didn't add a unit test because it would depend on the CI filesystem supportinguser.*xattrs.