@@ -481,6 +481,13 @@ static void reset_reg_range_values(struct bpf_reg_state *regs, u32 regno)
481
481
regs [regno ].max_value = BPF_REGISTER_MAX_RANGE ;
482
482
}
483
483
484
+ static void mark_reg_unknown_value_and_range (struct bpf_reg_state * regs ,
485
+ u32 regno )
486
+ {
487
+ mark_reg_unknown_value (regs , regno );
488
+ reset_reg_range_values (regs , regno );
489
+ }
490
+
484
491
enum reg_arg_type {
485
492
SRC_OP , /* register is used as source operand */
486
493
DST_OP , /* register is used as destination operand */
@@ -532,6 +539,7 @@ static bool is_spillable_regtype(enum bpf_reg_type type)
532
539
switch (type ) {
533
540
case PTR_TO_MAP_VALUE :
534
541
case PTR_TO_MAP_VALUE_OR_NULL :
542
+ case PTR_TO_MAP_VALUE_ADJ :
535
543
case PTR_TO_STACK :
536
544
case PTR_TO_CTX :
537
545
case PTR_TO_PACKET :
@@ -616,7 +624,8 @@ static int check_stack_read(struct bpf_verifier_state *state, int off, int size,
616
624
}
617
625
if (value_regno >= 0 )
618
626
/* have read misc data from the stack */
619
- mark_reg_unknown_value (state -> regs , value_regno );
627
+ mark_reg_unknown_value_and_range (state -> regs ,
628
+ value_regno );
620
629
return 0 ;
621
630
}
622
631
}
@@ -627,14 +636,59 @@ static int check_map_access(struct bpf_verifier_env *env, u32 regno, int off,
627
636
{
628
637
struct bpf_map * map = env -> cur_state .regs [regno ].map_ptr ;
629
638
630
- if (off < 0 || off + size > map -> value_size ) {
639
+ if (off < 0 || size <= 0 || off + size > map -> value_size ) {
631
640
verbose ("invalid access to map value, value_size=%d off=%d size=%d\n" ,
632
641
map -> value_size , off , size );
633
642
return - EACCES ;
634
643
}
635
644
return 0 ;
636
645
}
637
646
647
+ /* check read/write into an adjusted map element */
648
+ static int check_map_access_adj (struct bpf_verifier_env * env , u32 regno ,
649
+ int off , int size )
650
+ {
651
+ struct bpf_verifier_state * state = & env -> cur_state ;
652
+ struct bpf_reg_state * reg = & state -> regs [regno ];
653
+ int err ;
654
+
655
+ /* We adjusted the register to this map value, so we
656
+ * need to change off and size to min_value and max_value
657
+ * respectively to make sure our theoretical access will be
658
+ * safe.
659
+ */
660
+ if (log_level )
661
+ print_verifier_state (state );
662
+ env -> varlen_map_value_access = true;
663
+ /* The minimum value is only important with signed
664
+ * comparisons where we can't assume the floor of a
665
+ * value is 0. If we are using signed variables for our
666
+ * index'es we need to make sure that whatever we use
667
+ * will have a set floor within our range.
668
+ */
669
+ if (reg -> min_value < 0 ) {
670
+ verbose ("R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n" ,
671
+ regno );
672
+ return - EACCES ;
673
+ }
674
+ err = check_map_access (env , regno , reg -> min_value + off , size );
675
+ if (err ) {
676
+ verbose ("R%d min value is outside of the array range\n" ,
677
+ regno );
678
+ return err ;
679
+ }
680
+
681
+ /* If we haven't set a max value then we need to bail
682
+ * since we can't be sure we won't do bad things.
683
+ */
684
+ if (reg -> max_value == BPF_REGISTER_MAX_RANGE ) {
685
+ verbose ("R%d unbounded memory access, make sure to bounds check any array access into a map\n" ,
686
+ regno );
687
+ return - EACCES ;
688
+ }
689
+ return check_map_access (env , regno , reg -> max_value + off , size );
690
+ }
691
+
638
692
#define MAX_PACKET_OFF 0xffff
639
693
640
694
static bool may_access_direct_pkt_data (struct bpf_verifier_env * env ,
@@ -775,47 +829,13 @@ static int check_mem_access(struct bpf_verifier_env *env, u32 regno, int off,
775
829
return - EACCES ;
776
830
}
777
831
778
- /* If we adjusted the register to this map value at all then we
779
- * need to change off and size to min_value and max_value
780
- * respectively to make sure our theoretical access will be
781
- * safe.
782
- */
783
- if (reg -> type == PTR_TO_MAP_VALUE_ADJ ) {
784
- if (log_level )
785
- print_verifier_state (state );
786
- env -> varlen_map_value_access = true;
787
- /* The minimum value is only important with signed
788
- * comparisons where we can't assume the floor of a
789
- * value is 0. If we are using signed variables for our
790
- * index'es we need to make sure that whatever we use
791
- * will have a set floor within our range.
792
- */
793
- if (reg -> min_value < 0 ) {
794
- verbose ("R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n" ,
795
- regno );
796
- return - EACCES ;
797
- }
798
- err = check_map_access (env , regno , reg -> min_value + off ,
799
- size );
800
- if (err ) {
801
- verbose ("R%d min value is outside of the array range\n" ,
802
- regno );
803
- return err ;
804
- }
805
-
806
- /* If we haven't set a max value then we need to bail
807
- * since we can't be sure we won't do bad things.
808
- */
809
- if (reg -> max_value == BPF_REGISTER_MAX_RANGE ) {
810
- verbose ("R%d unbounded memory access, make sure to bounds check any array access into a map\n" ,
811
- regno );
812
- return - EACCES ;
813
- }
814
- off += reg -> max_value ;
815
- }
816
- err = check_map_access (env , regno , off , size );
832
+ if (reg -> type == PTR_TO_MAP_VALUE_ADJ )
833
+ err = check_map_access_adj (env , regno , off , size );
834
+ else
835
+ err = check_map_access (env , regno , off , size );
817
836
if (!err && t == BPF_READ && value_regno >= 0 )
818
- mark_reg_unknown_value (state -> regs , value_regno );
837
+ mark_reg_unknown_value_and_range (state -> regs ,
838
+ value_regno );
819
839
820
840
} else if (reg -> type == PTR_TO_CTX ) {
821
841
enum bpf_reg_type reg_type = UNKNOWN_VALUE ;
@@ -827,7 +847,8 @@ static int check_mem_access(struct bpf_verifier_env *env, u32 regno, int off,
827
847
}
828
848
err = check_ctx_access (env , off , size , t , & reg_type );
829
849
if (!err && t == BPF_READ && value_regno >= 0 ) {
830
- mark_reg_unknown_value (state -> regs , value_regno );
850
+ mark_reg_unknown_value_and_range (state -> regs ,
851
+ value_regno );
831
852
/* note that reg.[id|off|range] == 0 */
832
853
state -> regs [value_regno ].type = reg_type ;
833
854
}
@@ -860,7 +881,8 @@ static int check_mem_access(struct bpf_verifier_env *env, u32 regno, int off,
860
881
}
861
882
err = check_packet_access (env , regno , off , size );
862
883
if (!err && t == BPF_READ && value_regno >= 0 )
863
- mark_reg_unknown_value (state -> regs , value_regno );
884
+ mark_reg_unknown_value_and_range (state -> regs ,
885
+ value_regno );
864
886
} else {
865
887
verbose ("R%d invalid mem access '%s'\n" ,
866
888
regno , reg_type_str [reg -> type ]);
@@ -958,6 +980,25 @@ static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
958
980
return 0 ;
959
981
}
960
982
983
+ static int check_helper_mem_access (struct bpf_verifier_env * env , int regno ,
984
+ int access_size , bool zero_size_allowed ,
985
+ struct bpf_call_arg_meta * meta )
986
+ {
987
+ struct bpf_reg_state * regs = env -> cur_state .regs ;
988
+
989
+ switch (regs [regno ].type ) {
990
+ case PTR_TO_PACKET :
991
+ return check_packet_access (env , regno , 0 , access_size );
992
+ case PTR_TO_MAP_VALUE :
993
+ return check_map_access (env , regno , 0 , access_size );
994
+ case PTR_TO_MAP_VALUE_ADJ :
995
+ return check_map_access_adj (env , regno , 0 , access_size );
996
+ default : /* const_imm|ptr_to_stack or invalid ptr */
997
+ return check_stack_boundary (env , regno , access_size ,
998
+ zero_size_allowed , meta );
999
+ }
1000
+ }
1001
+
961
1002
static int check_func_arg (struct bpf_verifier_env * env , u32 regno ,
962
1003
enum bpf_arg_type arg_type ,
963
1004
struct bpf_call_arg_meta * meta )
@@ -993,10 +1034,13 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
993
1034
expected_type = PTR_TO_STACK ;
994
1035
if (type != PTR_TO_PACKET && type != expected_type )
995
1036
goto err_type ;
996
- } else if (arg_type == ARG_CONST_STACK_SIZE ||
997
- arg_type == ARG_CONST_STACK_SIZE_OR_ZERO ) {
1037
+ } else if (arg_type == ARG_CONST_SIZE ||
1038
+ arg_type == ARG_CONST_SIZE_OR_ZERO ) {
998
1039
expected_type = CONST_IMM ;
999
- if (type != expected_type )
1040
+ /* One exception. Allow UNKNOWN_VALUE registers when the
1041
+ * boundaries are known and don't cause unsafe memory accesses
1042
+ */
1043
+ if (type != UNKNOWN_VALUE && type != expected_type )
1000
1044
goto err_type ;
1001
1045
} else if (arg_type == ARG_CONST_MAP_PTR ) {
1002
1046
expected_type = CONST_PTR_TO_MAP ;
@@ -1006,18 +1050,19 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
1006
1050
expected_type = PTR_TO_CTX ;
1007
1051
if (type != expected_type )
1008
1052
goto err_type ;
1009
- } else if (arg_type == ARG_PTR_TO_STACK ||
1010
- arg_type == ARG_PTR_TO_RAW_STACK ) {
1053
+ } else if (arg_type == ARG_PTR_TO_MEM ||
1054
+ arg_type == ARG_PTR_TO_UNINIT_MEM ) {
1011
1055
expected_type = PTR_TO_STACK ;
1012
1056
/* One exception here. In case function allows for NULL to be
1013
1057
* passed in as argument, it's a CONST_IMM type. Final test
1014
1058
* happens during stack boundary checking.
1015
1059
*/
1016
1060
if (type == CONST_IMM && reg -> imm == 0 )
1017
1061
/* final test in check_stack_boundary() */ ;
1018
- else if (type != PTR_TO_PACKET && type != expected_type )
1062
+ else if (type != PTR_TO_PACKET && type != PTR_TO_MAP_VALUE &&
1063
+ type != PTR_TO_MAP_VALUE_ADJ && type != expected_type )
1019
1064
goto err_type ;
1020
- meta -> raw_mode = arg_type == ARG_PTR_TO_RAW_STACK ;
1065
+ meta -> raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM ;
1021
1066
} else {
1022
1067
verbose ("unsupported arg_type %d\n" , arg_type );
1023
1068
return - EFAULT ;
@@ -1063,24 +1108,60 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
1063
1108
err = check_stack_boundary (env , regno ,
1064
1109
meta -> map_ptr -> value_size ,
1065
1110
false, NULL );
1066
- } else if (arg_type == ARG_CONST_STACK_SIZE ||
1067
- arg_type == ARG_CONST_STACK_SIZE_OR_ZERO ) {
1068
- bool zero_size_allowed = (arg_type == ARG_CONST_STACK_SIZE_OR_ZERO );
1111
+ } else if (arg_type == ARG_CONST_SIZE ||
1112
+ arg_type == ARG_CONST_SIZE_OR_ZERO ) {
1113
+ bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO );
1069
1114
1070
1115
/* bpf_xxx(..., buf, len) call will access 'len' bytes
1071
1116
* from stack pointer 'buf'. Check it
1072
1117
* note: regno == len, regno - 1 == buf
1073
1118
*/
1074
1119
if (regno == 0 ) {
1075
1120
/* kernel subsystem misconfigured verifier */
1076
- verbose ("ARG_CONST_STACK_SIZE cannot be first argument\n" );
1121
+ verbose ("ARG_CONST_SIZE cannot be first argument\n" );
1077
1122
return - EACCES ;
1078
1123
}
1079
- if (regs [regno - 1 ].type == PTR_TO_PACKET )
1080
- err = check_packet_access (env , regno - 1 , 0 , reg -> imm );
1081
- else
1082
- err = check_stack_boundary (env , regno - 1 , reg -> imm ,
1083
- zero_size_allowed , meta );
1124
+
1125
+ /* If the register is UNKNOWN_VALUE, the access check happens
1126
+ * using its boundaries. Otherwise, just use its imm
1127
+ */
1128
+ if (type == UNKNOWN_VALUE ) {
1129
+ /* For unprivileged variable accesses, disable raw
1130
+ * mode so that the program is required to
1131
+ * initialize all the memory that the helper could
1132
+ * just partially fill up.
1133
+ */
1134
+ meta = NULL ;
1135
+
1136
+ if (reg -> min_value < 0 ) {
1137
+ verbose ("R%d min value is negative, either use unsigned or 'var &= const'\n" ,
1138
+ regno );
1139
+ return - EACCES ;
1140
+ }
1141
+
1142
+ if (reg -> min_value == 0 ) {
1143
+ err = check_helper_mem_access (env , regno - 1 , 0 ,
1144
+ zero_size_allowed ,
1145
+ meta );
1146
+ if (err )
1147
+ return err ;
1148
+ }
1149
+
1150
+ if (reg -> max_value == BPF_REGISTER_MAX_RANGE ) {
1151
+ verbose ("R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n" ,
1152
+ regno );
1153
+ return - EACCES ;
1154
+ }
1155
+ err = check_helper_mem_access (env , regno - 1 ,
1156
+ reg -> max_value ,
1157
+ zero_size_allowed , meta );
1158
+ if (err )
1159
+ return err ;
1160
+ } else {
1161
+ /* register is CONST_IMM */
1162
+ err = check_helper_mem_access (env , regno - 1 , reg -> imm ,
1163
+ zero_size_allowed , meta );
1164
+ }
1084
1165
}
1085
1166
1086
1167
return err ;
@@ -1154,15 +1235,15 @@ static int check_raw_mode(const struct bpf_func_proto *fn)
1154
1235
{
1155
1236
int count = 0 ;
1156
1237
1157
- if (fn -> arg1_type == ARG_PTR_TO_RAW_STACK )
1238
+ if (fn -> arg1_type == ARG_PTR_TO_UNINIT_MEM )
1158
1239
count ++ ;
1159
- if (fn -> arg2_type == ARG_PTR_TO_RAW_STACK )
1240
+ if (fn -> arg2_type == ARG_PTR_TO_UNINIT_MEM )
1160
1241
count ++ ;
1161
- if (fn -> arg3_type == ARG_PTR_TO_RAW_STACK )
1242
+ if (fn -> arg3_type == ARG_PTR_TO_UNINIT_MEM )
1162
1243
count ++ ;
1163
- if (fn -> arg4_type == ARG_PTR_TO_RAW_STACK )
1244
+ if (fn -> arg4_type == ARG_PTR_TO_UNINIT_MEM )
1164
1245
count ++ ;
1165
- if (fn -> arg5_type == ARG_PTR_TO_RAW_STACK )
1246
+ if (fn -> arg5_type == ARG_PTR_TO_UNINIT_MEM )
1166
1247
count ++ ;
1167
1248
1168
1249
return count > 1 ? - EINVAL : 0 ;
@@ -2729,7 +2810,6 @@ static int do_check(struct bpf_verifier_env *env)
2729
2810
if (err )
2730
2811
return err ;
2731
2812
2732
- reset_reg_range_values (regs , insn -> dst_reg );
2733
2813
if (BPF_SIZE (insn -> code ) != BPF_W &&
2734
2814
BPF_SIZE (insn -> code ) != BPF_DW ) {
2735
2815
insn_idx ++ ;
0 commit comments