@@ -150,16 +150,6 @@ static struct landlock_object *get_inode_object(struct inode *const inode)
150
150
LANDLOCK_ACCESS_FS_TRUNCATE)
151
151
/* clang-format on */
152
152
153
- /*
154
- * All access rights that are denied by default whether they are handled or not
155
- * by a ruleset/layer. This must be ORed with all ruleset->fs_access_masks[]
156
- * entries when we need to get the absolute handled access masks.
157
- */
158
- /* clang-format off */
159
- #define ACCESS_INITIALLY_DENIED ( \
160
- LANDLOCK_ACCESS_FS_REFER)
161
- /* clang-format on */
162
-
163
153
/*
164
154
* @path: Should have been checked by get_path_from_fd().
165
155
*/
@@ -179,8 +169,7 @@ int landlock_append_fs_rule(struct landlock_ruleset *const ruleset,
179
169
180
170
/* Transforms relative access rights to absolute ones. */
181
171
access_rights |= LANDLOCK_MASK_ACCESS_FS &
182
- ~(landlock_get_fs_access_mask (ruleset , 0 ) |
183
- ACCESS_INITIALLY_DENIED );
172
+ ~landlock_get_fs_access_mask (ruleset , 0 );
184
173
object = get_inode_object (d_backing_inode (path -> dentry ));
185
174
if (IS_ERR (object ))
186
175
return PTR_ERR (object );
@@ -287,15 +276,16 @@ static inline bool is_nouser_or_private(const struct dentry *dentry)
287
276
unlikely (IS_PRIVATE (d_backing_inode (dentry ))));
288
277
}
289
278
290
- static inline access_mask_t
291
- get_handled_accesses (const struct landlock_ruleset * const domain )
279
+ static access_mask_t
280
+ get_raw_handled_fs_accesses (const struct landlock_ruleset * const domain )
292
281
{
293
- access_mask_t access_dom = ACCESS_INITIALLY_DENIED ;
282
+ access_mask_t access_dom = 0 ;
294
283
size_t layer_level ;
295
284
296
285
for (layer_level = 0 ; layer_level < domain -> num_layers ; layer_level ++ )
297
- access_dom |= landlock_get_fs_access_mask (domain , layer_level );
298
- return access_dom & LANDLOCK_MASK_ACCESS_FS ;
286
+ access_dom |=
287
+ landlock_get_raw_fs_access_mask (domain , layer_level );
288
+ return access_dom ;
299
289
}
300
290
301
291
/**
@@ -331,13 +321,8 @@ init_layer_masks(const struct landlock_ruleset *const domain,
331
321
332
322
for_each_set_bit (access_bit , & access_req ,
333
323
ARRAY_SIZE (* layer_masks )) {
334
- /*
335
- * Artificially handles all initially denied by default
336
- * access rights.
337
- */
338
324
if (BIT_ULL (access_bit ) &
339
- (landlock_get_fs_access_mask (domain , layer_level ) |
340
- ACCESS_INITIALLY_DENIED )) {
325
+ landlock_get_fs_access_mask (domain , layer_level )) {
341
326
(* layer_masks )[access_bit ] |=
342
327
BIT_ULL (layer_level );
343
328
handled_accesses |= BIT_ULL (access_bit );
@@ -347,6 +332,25 @@ init_layer_masks(const struct landlock_ruleset *const domain,
347
332
return handled_accesses ;
348
333
}
349
334
335
+ static access_mask_t
336
+ get_handled_fs_accesses (const struct landlock_ruleset * const domain )
337
+ {
338
+ /* Handles all initially denied by default access rights. */
339
+ return get_raw_handled_fs_accesses (domain ) |
340
+ LANDLOCK_ACCESS_FS_INITIALLY_DENIED ;
341
+ }
342
+
343
+ static const struct landlock_ruleset * get_current_fs_domain (void )
344
+ {
345
+ const struct landlock_ruleset * const dom =
346
+ landlock_get_current_domain ();
347
+
348
+ if (!dom || !get_raw_handled_fs_accesses (dom ))
349
+ return NULL ;
350
+
351
+ return dom ;
352
+ }
353
+
350
354
/*
351
355
* Check that a destination file hierarchy has more restrictions than a source
352
356
* file hierarchy. This is only used for link and rename actions.
@@ -519,7 +523,7 @@ static bool is_access_to_paths_allowed(
519
523
* a superset of the meaningful requested accesses).
520
524
*/
521
525
access_masked_parent1 = access_masked_parent2 =
522
- get_handled_accesses (domain );
526
+ get_handled_fs_accesses (domain );
523
527
is_dom_check = true;
524
528
} else {
525
529
if (WARN_ON_ONCE (dentry_child1 || dentry_child2 ))
@@ -651,8 +655,7 @@ static inline int check_access_path(const struct landlock_ruleset *const domain,
651
655
static inline int current_check_access_path (const struct path * const path ,
652
656
const access_mask_t access_request )
653
657
{
654
- const struct landlock_ruleset * const dom =
655
- landlock_get_current_domain ();
658
+ const struct landlock_ruleset * const dom = get_current_fs_domain ();
656
659
657
660
if (!dom )
658
661
return 0 ;
@@ -815,8 +818,7 @@ static int current_check_refer_path(struct dentry *const old_dentry,
815
818
struct dentry * const new_dentry ,
816
819
const bool removable , const bool exchange )
817
820
{
818
- const struct landlock_ruleset * const dom =
819
- landlock_get_current_domain ();
821
+ const struct landlock_ruleset * const dom = get_current_fs_domain ();
820
822
bool allow_parent1 , allow_parent2 ;
821
823
access_mask_t access_request_parent1 , access_request_parent2 ;
822
824
struct path mnt_dir ;
@@ -1050,15 +1052,15 @@ static int hook_sb_mount(const char *const dev_name,
1050
1052
const struct path * const path , const char * const type ,
1051
1053
const unsigned long flags , void * const data )
1052
1054
{
1053
- if (!landlock_get_current_domain ())
1055
+ if (!get_current_fs_domain ())
1054
1056
return 0 ;
1055
1057
return - EPERM ;
1056
1058
}
1057
1059
1058
1060
static int hook_move_mount (const struct path * const from_path ,
1059
1061
const struct path * const to_path )
1060
1062
{
1061
- if (!landlock_get_current_domain ())
1063
+ if (!get_current_fs_domain ())
1062
1064
return 0 ;
1063
1065
return - EPERM ;
1064
1066
}
@@ -1069,14 +1071,14 @@ static int hook_move_mount(const struct path *const from_path,
1069
1071
*/
1070
1072
static int hook_sb_umount (struct vfsmount * const mnt , const int flags )
1071
1073
{
1072
- if (!landlock_get_current_domain ())
1074
+ if (!get_current_fs_domain ())
1073
1075
return 0 ;
1074
1076
return - EPERM ;
1075
1077
}
1076
1078
1077
1079
static int hook_sb_remount (struct super_block * const sb , void * const mnt_opts )
1078
1080
{
1079
- if (!landlock_get_current_domain ())
1081
+ if (!get_current_fs_domain ())
1080
1082
return 0 ;
1081
1083
return - EPERM ;
1082
1084
}
@@ -1092,7 +1094,7 @@ static int hook_sb_remount(struct super_block *const sb, void *const mnt_opts)
1092
1094
static int hook_sb_pivotroot (const struct path * const old_path ,
1093
1095
const struct path * const new_path )
1094
1096
{
1095
- if (!landlock_get_current_domain ())
1097
+ if (!get_current_fs_domain ())
1096
1098
return 0 ;
1097
1099
return - EPERM ;
1098
1100
}
@@ -1128,8 +1130,7 @@ static int hook_path_mknod(const struct path *const dir,
1128
1130
struct dentry * const dentry , const umode_t mode ,
1129
1131
const unsigned int dev )
1130
1132
{
1131
- const struct landlock_ruleset * const dom =
1132
- landlock_get_current_domain ();
1133
+ const struct landlock_ruleset * const dom = get_current_fs_domain ();
1133
1134
1134
1135
if (!dom )
1135
1136
return 0 ;
@@ -1208,8 +1209,7 @@ static int hook_file_open(struct file *const file)
1208
1209
layer_mask_t layer_masks [LANDLOCK_NUM_ACCESS_FS ] = {};
1209
1210
access_mask_t open_access_request , full_access_request , allowed_access ;
1210
1211
const access_mask_t optional_access = LANDLOCK_ACCESS_FS_TRUNCATE ;
1211
- const struct landlock_ruleset * const dom =
1212
- landlock_get_current_domain ();
1212
+ const struct landlock_ruleset * const dom = get_current_fs_domain ();
1213
1213
1214
1214
if (!dom )
1215
1215
return 0 ;
0 commit comments