|
@@ -1819,6 +1819,41 @@ static bool signed_sub_overflows(s64 a, s64 b)
|
|
return res > a;
|
|
return res > a;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+static bool check_reg_sane_offset(struct bpf_verifier_env *env,
|
|
|
|
+ const struct bpf_reg_state *reg,
|
|
|
|
+ enum bpf_reg_type type)
|
|
|
|
+{
|
|
|
|
+ bool known = tnum_is_const(reg->var_off);
|
|
|
|
+ s64 val = reg->var_off.value;
|
|
|
|
+ s64 smin = reg->smin_value;
|
|
|
|
+
|
|
|
|
+ if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) {
|
|
|
|
+ verbose(env, "math between %s pointer and %lld is not allowed\n",
|
|
|
|
+ reg_type_str[type], val);
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) {
|
|
|
|
+ verbose(env, "%s pointer offset %d is not allowed\n",
|
|
|
|
+ reg_type_str[type], reg->off);
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (smin == S64_MIN) {
|
|
|
|
+ verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n",
|
|
|
|
+ reg_type_str[type]);
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) {
|
|
|
|
+ verbose(env, "value %lld makes %s pointer be out of bounds\n",
|
|
|
|
+ smin, reg_type_str[type]);
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return true;
|
|
|
|
+}
|
|
|
|
+
|
|
/* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off.
|
|
/* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off.
|
|
* Caller should also handle BPF_MOV case separately.
|
|
* Caller should also handle BPF_MOV case separately.
|
|
* If we return -EACCES, caller may want to try again treating pointer as a
|
|
* If we return -EACCES, caller may want to try again treating pointer as a
|
|
@@ -1887,6 +1922,10 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
|
|
dst_reg->type = ptr_reg->type;
|
|
dst_reg->type = ptr_reg->type;
|
|
dst_reg->id = ptr_reg->id;
|
|
dst_reg->id = ptr_reg->id;
|
|
|
|
|
|
|
|
+ if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) ||
|
|
|
|
+ !check_reg_sane_offset(env, ptr_reg, ptr_reg->type))
|
|
|
|
+ return -EINVAL;
|
|
|
|
+
|
|
switch (opcode) {
|
|
switch (opcode) {
|
|
case BPF_ADD:
|
|
case BPF_ADD:
|
|
/* We can take a fixed offset as long as it doesn't overflow
|
|
/* We can take a fixed offset as long as it doesn't overflow
|
|
@@ -2017,6 +2056,9 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
|
|
return -EACCES;
|
|
return -EACCES;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type))
|
|
|
|
+ return -EINVAL;
|
|
|
|
+
|
|
__update_reg_bounds(dst_reg);
|
|
__update_reg_bounds(dst_reg);
|
|
__reg_deduce_bounds(dst_reg);
|
|
__reg_deduce_bounds(dst_reg);
|
|
__reg_bound_offset(dst_reg);
|
|
__reg_bound_offset(dst_reg);
|
|
@@ -2046,6 +2088,12 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
|
|
src_known = tnum_is_const(src_reg.var_off);
|
|
src_known = tnum_is_const(src_reg.var_off);
|
|
dst_known = tnum_is_const(dst_reg->var_off);
|
|
dst_known = tnum_is_const(dst_reg->var_off);
|
|
|
|
|
|
|
|
+ if (!src_known &&
|
|
|
|
+ opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) {
|
|
|
|
+ __mark_reg_unknown(dst_reg);
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
|
|
+
|
|
switch (opcode) {
|
|
switch (opcode) {
|
|
case BPF_ADD:
|
|
case BPF_ADD:
|
|
if (signed_add_overflows(dst_reg->smin_value, smin_val) ||
|
|
if (signed_add_overflows(dst_reg->smin_value, smin_val) ||
|