Skip to content

Commit 63817fe

Browse files
committed
landlock: Use f_cred in security_file_open() hook
Use landlock_cred(file->f_cred)->domain instead of landlock_get_current_domain() in security_file_open() hook implementation. This should not change the current behavior but could avoid potential race conditions in case of current task's credentials change. This will also ensure consistency with upcoming audit support relying on file->f_cred. Add and use a new get_fs_domain() helper to mask non-filesystem domains. file->f_cred is set by path_openat()/alloc_empty_file()/init_file() just before calling security_file_alloc(). Cc: Christian Brauner <[email protected]> Cc: Günther Noack <[email protected]> Cc: Jann Horn <[email protected]> Cc: Paul Moore <[email protected]> Reviewed-by: Kees Cook <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Mickaël Salaün <[email protected]>
1 parent e3e37fe commit 63817fe

File tree

1 file changed

+11
-7
lines changed
  • security/landlock

1 file changed

+11
-7
lines changed

security/landlock/fs.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,15 +248,18 @@ get_handled_fs_accesses(const struct landlock_ruleset *const domain)
248248
LANDLOCK_ACCESS_FS_INITIALLY_DENIED;
249249
}
250250

251-
static const struct landlock_ruleset *get_current_fs_domain(void)
251+
static const struct landlock_ruleset *
252+
get_fs_domain(const struct landlock_ruleset *const domain)
252253
{
253-
const struct landlock_ruleset *const dom =
254-
landlock_get_current_domain();
255-
256-
if (!dom || !get_raw_handled_fs_accesses(dom))
254+
if (!domain || !get_raw_handled_fs_accesses(domain))
257255
return NULL;
258256

259-
return dom;
257+
return domain;
258+
}
259+
260+
static const struct landlock_ruleset *get_current_fs_domain(void)
261+
{
262+
return get_fs_domain(landlock_get_current_domain());
260263
}
261264

262265
/*
@@ -1334,7 +1337,8 @@ static int hook_file_open(struct file *const file)
13341337
layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_FS] = {};
13351338
access_mask_t open_access_request, full_access_request, allowed_access;
13361339
const access_mask_t optional_access = LANDLOCK_ACCESS_FS_TRUNCATE;
1337-
const struct landlock_ruleset *const dom = get_current_fs_domain();
1340+
const struct landlock_ruleset *const dom =
1341+
get_fs_domain(landlock_cred(file->f_cred)->domain);
13381342

13391343
if (!dom)
13401344
return 0;

0 commit comments

Comments
 (0)