36
36
#include <uapi/linux/fiemap.h>
37
37
#include <uapi/linux/landlock.h>
38
38
39
+ #include "access.h"
39
40
#include "common.h"
40
41
#include "cred.h"
41
42
#include "fs.h"
@@ -388,14 +389,6 @@ static bool is_nouser_or_private(const struct dentry *dentry)
388
389
unlikely (IS_PRIVATE (d_backing_inode (dentry ))));
389
390
}
390
391
391
- static access_mask_t
392
- get_handled_fs_accesses (const struct landlock_ruleset * const domain )
393
- {
394
- /* Handles all initially denied by default access rights. */
395
- return landlock_union_access_masks (domain ).fs |
396
- LANDLOCK_ACCESS_FS_INITIALLY_DENIED ;
397
- }
398
-
399
392
static const struct access_masks any_fs = {
400
393
.fs = ~0 ,
401
394
};
@@ -572,6 +565,12 @@ static void test_no_more_access(struct kunit *const test)
572
565
#undef NMA_TRUE
573
566
#undef NMA_FALSE
574
567
568
+ static bool is_layer_masks_allowed (
569
+ layer_mask_t (* const layer_masks )[LANDLOCK_NUM_ACCESS_FS ])
570
+ {
571
+ return !memchr_inv (layer_masks , 0 , sizeof (* layer_masks ));
572
+ }
573
+
575
574
/*
576
575
* Removes @layer_masks accesses that are not requested.
577
576
*
@@ -589,7 +588,8 @@ scope_to_request(const access_mask_t access_request,
589
588
590
589
for_each_clear_bit (access_bit , & access_req , ARRAY_SIZE (* layer_masks ))
591
590
(* layer_masks )[access_bit ] = 0 ;
592
- return !memchr_inv (layer_masks , 0 , sizeof (* layer_masks ));
591
+
592
+ return is_layer_masks_allowed (layer_masks );
593
593
}
594
594
595
595
#ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST
@@ -778,16 +778,21 @@ static bool is_access_to_paths_allowed(
778
778
if (WARN_ON_ONCE (domain -> num_layers < 1 || !layer_masks_parent1 ))
779
779
return false;
780
780
781
+ allowed_parent1 = is_layer_masks_allowed (layer_masks_parent1 );
782
+
781
783
if (unlikely (layer_masks_parent2 )) {
782
784
if (WARN_ON_ONCE (!dentry_child1 ))
783
785
return false;
786
+
787
+ allowed_parent2 = is_layer_masks_allowed (layer_masks_parent2 );
788
+
784
789
/*
785
790
* For a double request, first check for potential privilege
786
791
* escalation by looking at domain handled accesses (which are
787
792
* a superset of the meaningful requested accesses).
788
793
*/
789
794
access_masked_parent1 = access_masked_parent2 =
790
- get_handled_fs_accesses (domain );
795
+ landlock_union_access_masks (domain ). fs ;
791
796
is_dom_check = true;
792
797
} else {
793
798
if (WARN_ON_ONCE (dentry_child1 || dentry_child2 ))
@@ -847,31 +852,39 @@ static bool is_access_to_paths_allowed(
847
852
child1_is_directory , layer_masks_parent2 ,
848
853
layer_masks_child2 ,
849
854
child2_is_directory ))) {
850
- allowed_parent1 = scope_to_request (
851
- access_request_parent1 , layer_masks_parent1 );
852
- allowed_parent2 = scope_to_request (
853
- access_request_parent2 , layer_masks_parent2 );
854
-
855
- /* Stops when all accesses are granted. */
856
- if (allowed_parent1 && allowed_parent2 )
857
- break ;
858
-
859
855
/*
860
856
* Now, downgrades the remaining checks from domain
861
857
* handled accesses to requested accesses.
862
858
*/
863
859
is_dom_check = false;
864
860
access_masked_parent1 = access_request_parent1 ;
865
861
access_masked_parent2 = access_request_parent2 ;
862
+
863
+ allowed_parent1 =
864
+ allowed_parent1 ||
865
+ scope_to_request (access_masked_parent1 ,
866
+ layer_masks_parent1 );
867
+ allowed_parent2 =
868
+ allowed_parent2 ||
869
+ scope_to_request (access_masked_parent2 ,
870
+ layer_masks_parent2 );
871
+
872
+ /* Stops when all accesses are granted. */
873
+ if (allowed_parent1 && allowed_parent2 )
874
+ break ;
866
875
}
867
876
868
877
rule = find_rule (domain , walker_path .dentry );
869
- allowed_parent1 = landlock_unmask_layers (
870
- rule , access_masked_parent1 , layer_masks_parent1 ,
871
- ARRAY_SIZE (* layer_masks_parent1 ));
872
- allowed_parent2 = landlock_unmask_layers (
873
- rule , access_masked_parent2 , layer_masks_parent2 ,
874
- ARRAY_SIZE (* layer_masks_parent2 ));
878
+ allowed_parent1 = allowed_parent1 ||
879
+ landlock_unmask_layers (
880
+ rule , access_masked_parent1 ,
881
+ layer_masks_parent1 ,
882
+ ARRAY_SIZE (* layer_masks_parent1 ));
883
+ allowed_parent2 = allowed_parent2 ||
884
+ landlock_unmask_layers (
885
+ rule , access_masked_parent2 ,
886
+ layer_masks_parent2 ,
887
+ ARRAY_SIZE (* layer_masks_parent2 ));
875
888
876
889
/* Stops when a rule from each layer grants access. */
877
890
if (allowed_parent1 && allowed_parent2 )
@@ -895,8 +908,10 @@ static bool is_access_to_paths_allowed(
895
908
* access to internal filesystems (e.g. nsfs, which is
896
909
* reachable through /proc/<pid>/ns/<namespace>).
897
910
*/
898
- allowed_parent1 = allowed_parent2 =
899
- !!(walker_path .mnt -> mnt_flags & MNT_INTERNAL );
911
+ if (walker_path .mnt -> mnt_flags & MNT_INTERNAL ) {
912
+ allowed_parent1 = true;
913
+ allowed_parent2 = true;
914
+ }
900
915
break ;
901
916
}
902
917
parent_dentry = dget_parent (walker_path .dentry );
@@ -908,39 +923,29 @@ static bool is_access_to_paths_allowed(
908
923
return allowed_parent1 && allowed_parent2 ;
909
924
}
910
925
911
- static int check_access_path (const struct landlock_ruleset * const domain ,
912
- const struct path * const path ,
913
- access_mask_t access_request )
914
- {
915
- layer_mask_t layer_masks [LANDLOCK_NUM_ACCESS_FS ] = {};
916
-
917
- access_request = landlock_init_layer_masks (
918
- domain , access_request , & layer_masks , LANDLOCK_KEY_INODE );
919
- if (is_access_to_paths_allowed (domain , path , access_request ,
920
- & layer_masks , NULL , 0 , NULL , NULL ))
921
- return 0 ;
922
- return - EACCES ;
923
- }
924
-
925
926
static int current_check_access_path (const struct path * const path ,
926
- const access_mask_t access_request )
927
+ access_mask_t access_request )
927
928
{
928
929
const struct landlock_ruleset * const dom = get_current_fs_domain ();
930
+ layer_mask_t layer_masks [LANDLOCK_NUM_ACCESS_FS ] = {};
929
931
930
932
if (!dom )
931
933
return 0 ;
932
- return check_access_path (dom , path , access_request );
934
+
935
+ access_request = landlock_init_layer_masks (
936
+ dom , access_request , & layer_masks , LANDLOCK_KEY_INODE );
937
+ if (is_access_to_paths_allowed (dom , path , access_request , & layer_masks ,
938
+ NULL , 0 , NULL , NULL ))
939
+ return 0 ;
940
+
941
+ return - EACCES ;
933
942
}
934
943
935
- static access_mask_t get_mode_access (const umode_t mode )
944
+ static __attribute_const__ access_mask_t get_mode_access (const umode_t mode )
936
945
{
937
946
switch (mode & S_IFMT ) {
938
947
case S_IFLNK :
939
948
return LANDLOCK_ACCESS_FS_MAKE_SYM ;
940
- case 0 :
941
- /* A zero mode translates to S_IFREG. */
942
- case S_IFREG :
943
- return LANDLOCK_ACCESS_FS_MAKE_REG ;
944
949
case S_IFDIR :
945
950
return LANDLOCK_ACCESS_FS_MAKE_DIR ;
946
951
case S_IFCHR :
@@ -951,9 +956,12 @@ static access_mask_t get_mode_access(const umode_t mode)
951
956
return LANDLOCK_ACCESS_FS_MAKE_FIFO ;
952
957
case S_IFSOCK :
953
958
return LANDLOCK_ACCESS_FS_MAKE_SOCK ;
959
+ case S_IFREG :
960
+ case 0 :
961
+ /* A zero mode translates to S_IFREG. */
954
962
default :
955
- WARN_ON_ONCE ( 1 );
956
- return 0 ;
963
+ /* Treats weird files as regular files. */
964
+ return LANDLOCK_ACCESS_FS_MAKE_REG ;
957
965
}
958
966
}
959
967
@@ -1414,11 +1422,7 @@ static int hook_path_mknod(const struct path *const dir,
1414
1422
struct dentry * const dentry , const umode_t mode ,
1415
1423
const unsigned int dev )
1416
1424
{
1417
- const struct landlock_ruleset * const dom = get_current_fs_domain ();
1418
-
1419
- if (!dom )
1420
- return 0 ;
1421
- return check_access_path (dom , dir , get_mode_access (mode ));
1425
+ return current_check_access_path (dir , get_mode_access (mode ));
1422
1426
}
1423
1427
1424
1428
static int hook_path_symlink (const struct path * const dir ,
0 commit comments