Skip to content

Commit 1f0b0f0

Browse files
mattbobrowskiKernel Patches Daemon
authored andcommitted
bpf: enforce VFS constraints for xattr related BPF kfuncs
Enforce VFS constraints and semantics regarding name and value lengths within the xattr related BPF kfuncs. Specifically, reject names that are empty or longer than XATTR_NAME_MAX, and values larger than XATTR_SIZE_MAX. Also validate the supplied flags to ensure that only XATTR_CREATE and XATTR_REPLACE can be used alongside the default flag value 0. Fixes: 5646729 ("bpf: fs/xattr: Add BPF kfuncs to set and remove xattrs") Closes: https://lore.kernel.org/bpf/20260429221005.6D1C6C19425@smtp.kernel.org/ Signed-off-by: Matt Bobrowski <mattbobrowski@google.com>
1 parent 0290554 commit 1f0b0f0

1 file changed

Lines changed: 70 additions & 15 deletions

File tree

fs/bpf_fs_kfuncs.c

Lines changed: 70 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,21 @@ __bpf_kfunc int bpf_path_d_path(const struct path *path, char *buf, size_t buf__
9393
return len;
9494
}
9595

96+
static int bpf_xattr_validate_name(const char *name)
97+
{
98+
u32 name_len;
99+
100+
/*
101+
* Impose the same restrictions on the supplied name as done so within
102+
* the VFS by helpers like import_xattr_name().
103+
*/
104+
name_len = strlen(name);
105+
if (!name_len || name_len > XATTR_NAME_MAX)
106+
return -ERANGE;
107+
108+
return 0;
109+
}
110+
96111
static bool match_security_bpf_prefix(const char *name__str)
97112
{
98113
return !strncmp(name__str, XATTR_NAME_BPF_LSM, XATTR_NAME_BPF_LSM_LEN);
@@ -117,10 +132,10 @@ static int bpf_xattr_read_permission(const char *name, struct inode *inode)
117132
* @name__str: name of the xattr
118133
* @value_p: output buffer of the xattr value
119134
*
120-
* Get xattr *name__str* of *dentry* and store the output in *value_ptr*.
135+
* Get xattr *name__str* of *dentry* and store the output in *value_p*.
121136
*
122-
* For security reasons, only *name__str* with prefixes "user." or
123-
* "security.bpf." are allowed.
137+
* For security reasons, only *name__str* values prefixed with "user." or
138+
* "security.bpf." are permitted.
124139
*
125140
* Return: length of the xattr value on success, a negative value on error.
126141
*/
@@ -133,6 +148,10 @@ __bpf_kfunc int bpf_get_dentry_xattr(struct dentry *dentry, const char *name__st
133148
void *value;
134149
int ret;
135150

151+
ret = bpf_xattr_validate_name(name__str);
152+
if (ret)
153+
return ret;
154+
136155
value_len = __bpf_dynptr_size(value_ptr);
137156
value = __bpf_dynptr_data_rw(value_ptr, value_len);
138157
if (!value)
@@ -150,10 +169,10 @@ __bpf_kfunc int bpf_get_dentry_xattr(struct dentry *dentry, const char *name__st
150169
* @name__str: name of the xattr
151170
* @value_p: output buffer of the xattr value
152171
*
153-
* Get xattr *name__str* of *file* and store the output in *value_ptr*.
172+
* Get xattr *name__str* of *file* and store the output in *value_p*.
154173
*
155-
* For security reasons, only *name__str* with prefixes "user." or
156-
* "security.bpf." are allowed.
174+
* For security reasons, only *name__str* values prefixed with "user." or
175+
* "security.bpf." are permitted.
157176
*
158177
* Return: length of the xattr value on success, a negative value on error.
159178
*/
@@ -187,10 +206,18 @@ static int bpf_xattr_write_permission(const char *name, struct inode *inode)
187206
* @value_p: xattr value
188207
* @flags: flags to pass into filesystem operations
189208
*
190-
* Set xattr *name__str* of *dentry* to the value in *value_ptr*.
209+
* Set xattr *name__str* of *dentry* to the value in *value_p*.
191210
*
192-
* For security reasons, only *name__str* with prefix "security.bpf."
193-
* is allowed.
211+
* For security reasons, only *name__str* values prefixed with "security.bpf."
212+
* are permitted.
213+
*
214+
* The length constraints imposed on both the xattr name and value abide those
215+
* that are also enforced by the VFS. Additionally, the flags argument respects
216+
* what's enforced by the VFS in the same way. By default, the flag value of 0
217+
* is permitted and an xattr will be created if it does not exist, or the value
218+
* will be replaced if the xattr already exists. More course grained control
219+
* over these exact semantics is permitted by explicitly specifying either
220+
* XATTR_CREATE or XATTR_REPLACE.
194221
*
195222
* The caller already locked dentry->d_inode.
196223
*
@@ -206,7 +233,17 @@ int bpf_set_dentry_xattr_locked(struct dentry *dentry, const char *name__str,
206233
u32 value_len;
207234
int ret;
208235

236+
if (flags & ~(XATTR_CREATE | XATTR_REPLACE))
237+
return -EINVAL;
238+
239+
ret = bpf_xattr_validate_name(name__str);
240+
if (ret)
241+
return ret;
242+
209243
value_len = __bpf_dynptr_size(value_ptr);
244+
if (value_len > XATTR_SIZE_MAX)
245+
return -E2BIG;
246+
210247
value = __bpf_dynptr_data(value_ptr, value_len);
211248
if (!value)
212249
return -EINVAL;
@@ -237,8 +274,8 @@ int bpf_set_dentry_xattr_locked(struct dentry *dentry, const char *name__str,
237274
*
238275
* Rmove xattr *name__str* of *dentry*.
239276
*
240-
* For security reasons, only *name__str* with prefix "security.bpf."
241-
* is allowed.
277+
* For security reasons, only *name__str* values prefixed with "security.bpf."
278+
* are permitted.
242279
*
243280
* The caller already locked dentry->d_inode.
244281
*
@@ -249,6 +286,10 @@ int bpf_remove_dentry_xattr_locked(struct dentry *dentry, const char *name__str)
249286
struct inode *inode = d_inode(dentry);
250287
int ret;
251288

289+
ret = bpf_xattr_validate_name(name__str);
290+
if (ret)
291+
return ret;
292+
252293
ret = bpf_xattr_write_permission(name__str, inode);
253294
if (ret)
254295
return ret;
@@ -274,11 +315,19 @@ __bpf_kfunc_start_defs();
274315
* @value_p: xattr value
275316
* @flags: flags to pass into filesystem operations
276317
*
277-
* Set xattr *name__str* of *dentry* to the value in *value_ptr*.
318+
* Set xattr *name__str* of *dentry* to the value in *value_p*.
278319
*
279320
* For security reasons, only *name__str* with prefix "security.bpf."
280321
* is allowed.
281322
*
323+
* The length constraints imposed on both the xattr name and value abide those
324+
* that are also enforced by the VFS. Additionally, the flags argument respects
325+
* what's enforced by the VFS in the same way. By default, the flag value of 0
326+
* is permitted and an xattr will be created if it does not exist, or the value
327+
* will be replaced if the xattr already exists. More course grained control
328+
* over these exact semantics is permitted by explicitly specifying either
329+
* XATTR_CREATE or XATTR_REPLACE.
330+
*
282331
* The caller has not locked dentry->d_inode.
283332
*
284333
* Return: 0 on success, a negative value on error.
@@ -327,18 +376,24 @@ __bpf_kfunc int bpf_remove_dentry_xattr(struct dentry *dentry, const char *name_
327376
* @name__str: name of the xattr
328377
* @value_p: output buffer of the xattr value
329378
*
330-
* Get xattr *name__str* of *cgroup* and store the output in *value_ptr*.
379+
* Get xattr *name__str* of *cgroup* and store the output in *value_p*.
331380
*
332-
* For security reasons, only *name__str* with prefix "user." is allowed.
381+
* For security reasons, only *name__str* values prefixed with "user." are
382+
* permitted.
333383
*
334384
* Return: length of the xattr value on success, a negative value on error.
335385
*/
336386
__bpf_kfunc int bpf_cgroup_read_xattr(struct cgroup *cgroup, const char *name__str,
337-
struct bpf_dynptr *value_p)
387+
struct bpf_dynptr *value_p)
338388
{
339389
struct bpf_dynptr_kern *value_ptr = (struct bpf_dynptr_kern *)value_p;
340390
u32 value_len;
341391
void *value;
392+
int ret;
393+
394+
ret = bpf_xattr_validate_name(name__str);
395+
if (ret)
396+
return ret;
342397

343398
/* Only allow reading "user.*" xattrs */
344399
if (strncmp(name__str, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))

0 commit comments

Comments
 (0)