Skip to content

Commit 45f2a29

Browse files
gnoackl0kod
authored andcommitted
landlock: Add access_mask_subset() helper
This helper function checks whether an access_mask_t has a subset of the bits enabled than another one. This expresses the intent a bit smoother in the code and does not cost us anything when it gets inlined. Signed-off-by: Günther Noack <gnoack3000@gmail.com> Link: https://lore.kernel.org/r/20260206151154.97915-4-gnoack3000@gmail.com [mic: Improve subject] Signed-off-by: Mickaël Salaün <mic@digikod.net>
1 parent 9adbe89 commit 45f2a29

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

security/landlock/access.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,11 @@ landlock_upgrade_handled_access_masks(struct access_masks access_masks)
9797
return access_masks;
9898
}
9999

100+
/* Checks the subset relation between access masks. */
101+
static inline bool access_mask_subset(access_mask_t subset,
102+
access_mask_t superset)
103+
{
104+
return (subset | superset) == superset;
105+
}
106+
100107
#endif /* _SECURITY_LANDLOCK_ACCESS_H */

security/landlock/fs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ int landlock_append_fs_rule(struct landlock_ruleset *const ruleset,
331331

332332
/* Files only get access rights that make sense. */
333333
if (!d_is_dir(path->dentry) &&
334-
(access_rights | ACCESS_FILE) != ACCESS_FILE)
334+
!access_mask_subset(access_rights, ACCESS_FILE))
335335
return -EINVAL;
336336
if (WARN_ON_ONCE(ruleset->num_layers != 1))
337337
return -EINVAL;
@@ -1704,7 +1704,7 @@ static int hook_file_open(struct file *const file)
17041704
ARRAY_SIZE(layer_masks));
17051705
#endif /* CONFIG_AUDIT */
17061706

1707-
if ((open_access_request & allowed_access) == open_access_request)
1707+
if (access_mask_subset(open_access_request, allowed_access))
17081708
return 0;
17091709

17101710
/* Sets access to reflect the actual request. */

0 commit comments

Comments
 (0)