Commit 5f2ff33e authored by Mickaël Salaün's avatar Mickaël Salaün

landlock: Define access_mask_t to enforce a consistent access mask size

Create and use the access_mask_t typedef to enforce a consistent access
mask size and uniformly use a 16-bits type.  This will helps transition
to a 32-bits value one day.

Add a build check to make sure all (filesystem) access rights fit in.
This will be extended with a following commit.
Reviewed-by: default avatarPaul Moore <paul@paul-moore.com>
Link: https://lore.kernel.org/r/20220506161102.525323-2-mic@digikod.net
Cc: stable@vger.kernel.org
Signed-off-by: default avatarMickaël Salaün <mic@digikod.net>
parent 6533d0c3
...@@ -152,7 +152,8 @@ static struct landlock_object *get_inode_object(struct inode *const inode) ...@@ -152,7 +152,8 @@ static struct landlock_object *get_inode_object(struct inode *const inode)
* @path: Should have been checked by get_path_from_fd(). * @path: Should have been checked by get_path_from_fd().
*/ */
int landlock_append_fs_rule(struct landlock_ruleset *const ruleset, int landlock_append_fs_rule(struct landlock_ruleset *const ruleset,
const struct path *const path, u32 access_rights) const struct path *const path,
access_mask_t access_rights)
{ {
int err; int err;
struct landlock_object *object; struct landlock_object *object;
...@@ -184,7 +185,8 @@ int landlock_append_fs_rule(struct landlock_ruleset *const ruleset, ...@@ -184,7 +185,8 @@ int landlock_append_fs_rule(struct landlock_ruleset *const ruleset,
static inline u64 unmask_layers(const struct landlock_ruleset *const domain, static inline u64 unmask_layers(const struct landlock_ruleset *const domain,
const struct path *const path, const struct path *const path,
const u32 access_request, u64 layer_mask) const access_mask_t access_request,
u64 layer_mask)
{ {
const struct landlock_rule *rule; const struct landlock_rule *rule;
const struct inode *inode; const struct inode *inode;
...@@ -224,7 +226,8 @@ static inline u64 unmask_layers(const struct landlock_ruleset *const domain, ...@@ -224,7 +226,8 @@ static inline u64 unmask_layers(const struct landlock_ruleset *const domain,
} }
static int check_access_path(const struct landlock_ruleset *const domain, static int check_access_path(const struct landlock_ruleset *const domain,
const struct path *const path, u32 access_request) const struct path *const path,
const access_mask_t access_request)
{ {
bool allowed = false; bool allowed = false;
struct path walker_path; struct path walker_path;
...@@ -309,7 +312,7 @@ static int check_access_path(const struct landlock_ruleset *const domain, ...@@ -309,7 +312,7 @@ static int check_access_path(const struct landlock_ruleset *const domain,
} }
static inline int current_check_access_path(const struct path *const path, static inline int current_check_access_path(const struct path *const path,
const u32 access_request) const access_mask_t access_request)
{ {
const struct landlock_ruleset *const dom = const struct landlock_ruleset *const dom =
landlock_get_current_domain(); landlock_get_current_domain();
...@@ -512,7 +515,7 @@ static int hook_sb_pivotroot(const struct path *const old_path, ...@@ -512,7 +515,7 @@ static int hook_sb_pivotroot(const struct path *const old_path,
/* Path hooks */ /* Path hooks */
static inline u32 get_mode_access(const umode_t mode) static inline access_mask_t get_mode_access(const umode_t mode)
{ {
switch (mode & S_IFMT) { switch (mode & S_IFMT) {
case S_IFLNK: case S_IFLNK:
...@@ -565,7 +568,7 @@ static int hook_path_link(struct dentry *const old_dentry, ...@@ -565,7 +568,7 @@ static int hook_path_link(struct dentry *const old_dentry,
get_mode_access(d_backing_inode(old_dentry)->i_mode)); get_mode_access(d_backing_inode(old_dentry)->i_mode));
} }
static inline u32 maybe_remove(const struct dentry *const dentry) static inline access_mask_t maybe_remove(const struct dentry *const dentry)
{ {
if (d_is_negative(dentry)) if (d_is_negative(dentry))
return 0; return 0;
...@@ -635,9 +638,9 @@ static int hook_path_rmdir(const struct path *const dir, ...@@ -635,9 +638,9 @@ static int hook_path_rmdir(const struct path *const dir,
/* File hooks */ /* File hooks */
static inline u32 get_file_access(const struct file *const file) static inline access_mask_t get_file_access(const struct file *const file)
{ {
u32 access = 0; access_mask_t access = 0;
if (file->f_mode & FMODE_READ) { if (file->f_mode & FMODE_READ) {
/* A directory can only be opened in read mode. */ /* A directory can only be opened in read mode. */
......
...@@ -66,6 +66,6 @@ __init void landlock_add_fs_hooks(void); ...@@ -66,6 +66,6 @@ __init void landlock_add_fs_hooks(void);
int landlock_append_fs_rule(struct landlock_ruleset *const ruleset, int landlock_append_fs_rule(struct landlock_ruleset *const ruleset,
const struct path *const path, const struct path *const path,
u32 access_hierarchy); access_mask_t access_hierarchy);
#endif /* _SECURITY_LANDLOCK_FS_H */ #endif /* _SECURITY_LANDLOCK_FS_H */
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#ifndef _SECURITY_LANDLOCK_LIMITS_H #ifndef _SECURITY_LANDLOCK_LIMITS_H
#define _SECURITY_LANDLOCK_LIMITS_H #define _SECURITY_LANDLOCK_LIMITS_H
#include <linux/bitops.h>
#include <linux/limits.h> #include <linux/limits.h>
#include <uapi/linux/landlock.h> #include <uapi/linux/landlock.h>
...@@ -19,6 +20,7 @@ ...@@ -19,6 +20,7 @@
#define LANDLOCK_LAST_ACCESS_FS LANDLOCK_ACCESS_FS_MAKE_SYM #define LANDLOCK_LAST_ACCESS_FS LANDLOCK_ACCESS_FS_MAKE_SYM
#define LANDLOCK_MASK_ACCESS_FS ((LANDLOCK_LAST_ACCESS_FS << 1) - 1) #define LANDLOCK_MASK_ACCESS_FS ((LANDLOCK_LAST_ACCESS_FS << 1) - 1)
#define LANDLOCK_NUM_ACCESS_FS __const_hweight64(LANDLOCK_MASK_ACCESS_FS)
/* clang-format on */ /* clang-format on */
......
...@@ -45,7 +45,8 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers) ...@@ -45,7 +45,8 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
return new_ruleset; return new_ruleset;
} }
struct landlock_ruleset *landlock_create_ruleset(const u32 fs_access_mask) struct landlock_ruleset *
landlock_create_ruleset(const access_mask_t fs_access_mask)
{ {
struct landlock_ruleset *new_ruleset; struct landlock_ruleset *new_ruleset;
...@@ -228,7 +229,8 @@ static void build_check_layer(void) ...@@ -228,7 +229,8 @@ static void build_check_layer(void)
/* @ruleset must be locked by the caller. */ /* @ruleset must be locked by the caller. */
int landlock_insert_rule(struct landlock_ruleset *const ruleset, int landlock_insert_rule(struct landlock_ruleset *const ruleset,
struct landlock_object *const object, const u32 access) struct landlock_object *const object,
const access_mask_t access)
{ {
struct landlock_layer layers[] = { { struct landlock_layer layers[] = { {
.access = access, .access = access,
......
...@@ -9,13 +9,20 @@ ...@@ -9,13 +9,20 @@
#ifndef _SECURITY_LANDLOCK_RULESET_H #ifndef _SECURITY_LANDLOCK_RULESET_H
#define _SECURITY_LANDLOCK_RULESET_H #define _SECURITY_LANDLOCK_RULESET_H
#include <linux/bitops.h>
#include <linux/build_bug.h>
#include <linux/mutex.h> #include <linux/mutex.h>
#include <linux/rbtree.h> #include <linux/rbtree.h>
#include <linux/refcount.h> #include <linux/refcount.h>
#include <linux/workqueue.h> #include <linux/workqueue.h>
#include "limits.h"
#include "object.h" #include "object.h"
typedef u16 access_mask_t;
/* Makes sure all filesystem access rights can be stored. */
static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_FS);
/** /**
* struct landlock_layer - Access rights for a given layer * struct landlock_layer - Access rights for a given layer
*/ */
...@@ -28,7 +35,7 @@ struct landlock_layer { ...@@ -28,7 +35,7 @@ struct landlock_layer {
* @access: Bitfield of allowed actions on the kernel object. They are * @access: Bitfield of allowed actions on the kernel object. They are
* relative to the object type (e.g. %LANDLOCK_ACTION_FS_READ). * relative to the object type (e.g. %LANDLOCK_ACTION_FS_READ).
*/ */
u16 access; access_mask_t access;
}; };
/** /**
...@@ -135,19 +142,20 @@ struct landlock_ruleset { ...@@ -135,19 +142,20 @@ struct landlock_ruleset {
* layers are set once and never changed for the * layers are set once and never changed for the
* lifetime of the ruleset. * lifetime of the ruleset.
*/ */
u16 fs_access_masks[]; access_mask_t fs_access_masks[];
}; };
}; };
}; };
struct landlock_ruleset *landlock_create_ruleset(const u32 fs_access_mask); struct landlock_ruleset *
landlock_create_ruleset(const access_mask_t fs_access_mask);
void landlock_put_ruleset(struct landlock_ruleset *const ruleset); void landlock_put_ruleset(struct landlock_ruleset *const ruleset);
void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset); void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset);
int landlock_insert_rule(struct landlock_ruleset *const ruleset, int landlock_insert_rule(struct landlock_ruleset *const ruleset,
struct landlock_object *const object, struct landlock_object *const object,
const u32 access); const access_mask_t access);
struct landlock_ruleset * struct landlock_ruleset *
landlock_merge_ruleset(struct landlock_ruleset *const parent, landlock_merge_ruleset(struct landlock_ruleset *const parent,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment