History

bpf: Verifer, refactor adjust_scalar_min_max_vals

Pull per op ALU logic into individual functions. We are about to add u32 versions of each of these by pull them out the code gets a bit more readable here and nicer in the next patch.

John Fastabend committed 6 years ago

Commit 07cd2631 parent 8395f321 file · +239 −164Browse files

Unified · Split

kernel/bpf/verifier.c+239−164
@@ -4843,6 +4843,237 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, · 4842 unmodified lines
48434843 return 0;
48444844 }
48454845
4846+static void scalar_min_max_add(struct bpf_reg_state *dst_reg,
4847+ struct bpf_reg_state *src_reg)
4848+{
4849+ s64 smin_val = src_reg->smin_value;
4850+ s64 smax_val = src_reg->smax_value;
4851+ u64 umin_val = src_reg->umin_value;
4852+ u64 umax_val = src_reg->umax_value;
4853+
4854+ if (signed_add_overflows(dst_reg->smin_value, smin_val) ||
4855+ signed_add_overflows(dst_reg->smax_value, smax_val)) {
4856+ dst_reg->smin_value = S64_MIN;
4857+ dst_reg->smax_value = S64_MAX;
4858+ } else {
4859+ dst_reg->smin_value += smin_val;
4860+ dst_reg->smax_value += smax_val;
4861+ }
4862+ if (dst_reg->umin_value + umin_val < umin_val ||
4863+ dst_reg->umax_value + umax_val < umax_val) {
4864+ dst_reg->umin_value = 0;
4865+ dst_reg->umax_value = U64_MAX;
4866+ } else {
4867+ dst_reg->umin_value += umin_val;
4868+ dst_reg->umax_value += umax_val;
4869+ }
4870+ dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg->var_off);
4871+}
4872+
4873+static void scalar_min_max_sub(struct bpf_reg_state *dst_reg,
4874+ struct bpf_reg_state *src_reg)
4875+{
4876+ s64 smin_val = src_reg->smin_value;
4877+ s64 smax_val = src_reg->smax_value;
4878+ u64 umin_val = src_reg->umin_value;
4879+ u64 umax_val = src_reg->umax_value;
4880+
4881+ if (signed_sub_overflows(dst_reg->smin_value, smax_val) ||
4882+ signed_sub_overflows(dst_reg->smax_value, smin_val)) {
4883+ /* Overflow possible, we know nothing */
4884+ dst_reg->smin_value = S64_MIN;
4885+ dst_reg->smax_value = S64_MAX;
4886+ } else {
4887+ dst_reg->smin_value -= smax_val;
4888+ dst_reg->smax_value -= smin_val;
4889+ }
4890+ if (dst_reg->umin_value < umax_val) {
4891+ /* Overflow possible, we know nothing */
4892+ dst_reg->umin_value = 0;
4893+ dst_reg->umax_value = U64_MAX;
4894+ } else {
4895+ /* Cannot overflow (as long as bounds are consistent) */
4896+ dst_reg->umin_value -= umax_val;
4897+ dst_reg->umax_value -= umin_val;
4898+ }
4899+ dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg->var_off);
4900+}
4901+
4902+static void scalar_min_max_mul(struct bpf_reg_state *dst_reg,
4903+ struct bpf_reg_state *src_reg)
4904+{
4905+ s64 smin_val = src_reg->smin_value;
4906+ u64 umin_val = src_reg->umin_value;
4907+ u64 umax_val = src_reg->umax_value;
4908+
4909+ dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg->var_off);
4910+ if (smin_val < 0 || dst_reg->smin_value < 0) {
4911+ /* Ain't nobody got time to multiply that sign */
4912+ __mark_reg_unbounded(dst_reg);
4913+ __update_reg_bounds(dst_reg);
4914+ return;
4915+ }
4916+ /* Both values are positive, so we can work with unsigned and
4917+ * copy the result to signed (unless it exceeds S64_MAX).
4918+ */
4919+ if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) {
4920+ /* Potential overflow, we know nothing */
4921+ __mark_reg_unbounded(dst_reg);
4922+ /* (except what we can learn from the var_off) */
4923+ __update_reg_bounds(dst_reg);
4924+ return;
4925+ }
4926+ dst_reg->umin_value *= umin_val;
4927+ dst_reg->umax_value *= umax_val;
4928+ if (dst_reg->umax_value > S64_MAX) {
4929+ /* Overflow possible, we know nothing */
4930+ dst_reg->smin_value = S64_MIN;
4931+ dst_reg->smax_value = S64_MAX;
4932+ } else {
4933+ dst_reg->smin_value = dst_reg->umin_value;
4934+ dst_reg->smax_value = dst_reg->umax_value;
4935+ }
4936+}
4937+
4938+static void scalar_min_max_and(struct bpf_reg_state *dst_reg,
4939+ struct bpf_reg_state *src_reg)
4940+{
4941+ s64 smin_val = src_reg->smin_value;
4942+ u64 umax_val = src_reg->umax_value;
4943+
4944+ /* We get our minimum from the var_off, since that's inherently
4945+ * bitwise. Our maximum is the minimum of the operands' maxima.
4946+ */
4947+ dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg->var_off);
4948+ dst_reg->umin_value = dst_reg->var_off.value;
4949+ dst_reg->umax_value = min(dst_reg->umax_value, umax_val);
4950+ if (dst_reg->smin_value < 0 || smin_val < 0) {
4951+ /* Lose signed bounds when ANDing negative numbers,
4952+ * ain't nobody got time for that.
4953+ */
4954+ dst_reg->smin_value = S64_MIN;
4955+ dst_reg->smax_value = S64_MAX;
4956+ } else {
4957+ /* ANDing two positives gives a positive, so safe to
4958+ * cast result into s64.
4959+ */
4960+ dst_reg->smin_value = dst_reg->umin_value;
4961+ dst_reg->smax_value = dst_reg->umax_value;
4962+ }
4963+ /* We may learn something more from the var_off */
4964+ __update_reg_bounds(dst_reg);
4965+}
4966+
4967+static void scalar_min_max_or(struct bpf_reg_state *dst_reg,
4968+ struct bpf_reg_state *src_reg)
4969+{
4970+ s64 smin_val = src_reg->smin_value;
4971+ u64 umin_val = src_reg->umin_value;
4972+
4973+ /* We get our maximum from the var_off, and our minimum is the
4974+ * maximum of the operands' minima
4975+ */
4976+ dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg->var_off);
4977+ dst_reg->umin_value = max(dst_reg->umin_value, umin_val);
4978+ dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask;
4979+ if (dst_reg->smin_value < 0 || smin_val < 0) {
4980+ /* Lose signed bounds when ORing negative numbers,
4981+ * ain't nobody got time for that.
4982+ */
4983+ dst_reg->smin_value = S64_MIN;
4984+ dst_reg->smax_value = S64_MAX;
4985+ } else {
4986+ /* ORing two positives gives a positive, so safe to
4987+ * cast result into s64.
4988+ */
4989+ dst_reg->smin_value = dst_reg->umin_value;
4990+ dst_reg->smax_value = dst_reg->umax_value;
4991+ }
4992+ /* We may learn something more from the var_off */
4993+ __update_reg_bounds(dst_reg);
4994+}
4995+
4996+static void scalar_min_max_lsh(struct bpf_reg_state *dst_reg,
4997+ struct bpf_reg_state *src_reg)
4998+{
4999+ u64 umax_val = src_reg->umax_value;
5000+ u64 umin_val = src_reg->umin_value;
5001+
5002+ /* We lose all sign bit information (except what we can pick
5003+ * up from var_off)
5004+ */
5005+ dst_reg->smin_value = S64_MIN;
5006+ dst_reg->smax_value = S64_MAX;
5007+ /* If we might shift our top bit out, then we know nothing */
5008+ if (dst_reg->umax_value > 1ULL << (63 - umax_val)) {
5009+ dst_reg->umin_value = 0;
5010+ dst_reg->umax_value = U64_MAX;
5011+ } else {
5012+ dst_reg->umin_value <<= umin_val;
5013+ dst_reg->umax_value <<= umax_val;
5014+ }
5015+ dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val);
5016+ /* We may learn something more from the var_off */
5017+ __update_reg_bounds(dst_reg);
5018+}
5019+
5020+static void scalar_min_max_rsh(struct bpf_reg_state *dst_reg,
5021+ struct bpf_reg_state *src_reg)
5022+{
5023+ u64 umax_val = src_reg->umax_value;
5024+ u64 umin_val = src_reg->umin_value;
5025+
5026+ /* BPF_RSH is an unsigned shift. If the value in dst_reg might
5027+ * be negative, then either:
5028+ * 1) src_reg might be zero, so the sign bit of the result is
5029+ * unknown, so we lose our signed bounds
5030+ * 2) it's known negative, thus the unsigned bounds capture the
5031+ * signed bounds
5032+ * 3) the signed bounds cross zero, so they tell us nothing
5033+ * about the result
5034+ * If the value in dst_reg is known nonnegative, then again the
5035+ * unsigned bounts capture the signed bounds.
5036+ * Thus, in all cases it suffices to blow away our signed bounds
5037+ * and rely on inferring new ones from the unsigned bounds and
5038+ * var_off of the result.
5039+ */
5040+ dst_reg->smin_value = S64_MIN;
5041+ dst_reg->smax_value = S64_MAX;
5042+ dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val);
5043+ dst_reg->umin_value >>= umax_val;
5044+ dst_reg->umax_value >>= umin_val;
5045+ /* We may learn something more from the var_off */
5046+ __update_reg_bounds(dst_reg);
5047+}
5048+
5049+static void scalar_min_max_arsh(struct bpf_reg_state *dst_reg,
5050+ struct bpf_reg_state *src_reg,
5051+ u64 insn_bitness)
5052+{
5053+ u64 umin_val = src_reg->umin_value;
5054+
5055+ /* Upon reaching here, src_known is true and
5056+ * umax_val is equal to umin_val.
5057+ */
5058+ if (insn_bitness == 32) {
5059+ dst_reg->smin_value = (u32)(((s32)dst_reg->smin_value) >> umin_val);
5060+ dst_reg->smax_value = (u32)(((s32)dst_reg->smax_value) >> umin_val);
5061+ } else {
5062+ dst_reg->smin_value >>= umin_val;
5063+ dst_reg->smax_value >>= umin_val;
5064+ }
5065+
5066+ dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val,
5067+ insn_bitness);
5068+
5069+ /* blow away the dst_reg umin_value/umax_value and rely on
5070+ * dst_reg var_off to refine the result.
5071+ */
5072+ dst_reg->umin_value = 0;
5073+ dst_reg->umax_value = U64_MAX;
5074+ __update_reg_bounds(dst_reg);
5075+}
5076+
48465077 /* WARNING: This function does calculations on 64-bit values, but the actual
48475078 * execution may occur on 32-bit values. Therefore, things like bitshifts
48485079 * need extra checks in the 32-bit case.
@@ -4899,23 +5130,7 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, · 50 unmodified lines
48995130 verbose(env, "R%d tried to add from different pointers or scalars\n", dst);
49005131 return ret;
49015132 }
4902− if (signed_add_overflows(dst_reg->smin_value, smin_val) ||
4903− signed_add_overflows(dst_reg->smax_value, smax_val)) {
4904− dst_reg->smin_value = S64_MIN;
4905− dst_reg->smax_value = S64_MAX;
4906− } else {
4907− dst_reg->smin_value += smin_val;
4908− dst_reg->smax_value += smax_val;
4909− }
4910− if (dst_reg->umin_value + umin_val < umin_val ||
4911− dst_reg->umax_value + umax_val < umax_val) {
4912− dst_reg->umin_value = 0;
4913− dst_reg->umax_value = U64_MAX;
4914− } else {
4915− dst_reg->umin_value += umin_val;
4916− dst_reg->umax_value += umax_val;
4917− }
4918− dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off);
5133+ scalar_min_max_add(dst_reg, &src_reg);
49195134 break;
49205135 case BPF_SUB:
49215136 ret = sanitize_val_alu(env, insn);
@@ -4923,54 +5138,10 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, · 1 unmodified line
49235138 verbose(env, "R%d tried to sub from different pointers or scalars\n", dst);
49245139 return ret;
49255140 }
4926− if (signed_sub_overflows(dst_reg->smin_value, smax_val) ||
4927− signed_sub_overflows(dst_reg->smax_value, smin_val)) {
4928− /* Overflow possible, we know nothing */
4929− dst_reg->smin_value = S64_MIN;
4930− dst_reg->smax_value = S64_MAX;
4931− } else {
4932− dst_reg->smin_value -= smax_val;
4933− dst_reg->smax_value -= smin_val;
4934− }
4935− if (dst_reg->umin_value < umax_val) {
4936− /* Overflow possible, we know nothing */
4937− dst_reg->umin_value = 0;
4938− dst_reg->umax_value = U64_MAX;
4939− } else {
4940− /* Cannot overflow (as long as bounds are consistent) */
4941− dst_reg->umin_value -= umax_val;
4942− dst_reg->umax_value -= umin_val;
4943− }
4944− dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off);
5141+ scalar_min_max_sub(dst_reg, &src_reg);
49455142 break;
49465143 case BPF_MUL:
4947− dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off);
4948− if (smin_val < 0 || dst_reg->smin_value < 0) {
4949− /* Ain't nobody got time to multiply that sign */
4950− __mark_reg_unbounded(dst_reg);
4951− __update_reg_bounds(dst_reg);
4952− break;
4953− }
4954− /* Both values are positive, so we can work with unsigned and
4955− * copy the result to signed (unless it exceeds S64_MAX).
4956− */
4957− if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) {
4958− /* Potential overflow, we know nothing */
4959− __mark_reg_unbounded(dst_reg);
4960− /* (except what we can learn from the var_off) */
4961− __update_reg_bounds(dst_reg);
4962− break;
4963− }
4964− dst_reg->umin_value *= umin_val;
4965− dst_reg->umax_value *= umax_val;
4966− if (dst_reg->umax_value > S64_MAX) {
4967− /* Overflow possible, we know nothing */
4968− dst_reg->smin_value = S64_MIN;
4969− dst_reg->smax_value = S64_MAX;
4970− } else {
4971− dst_reg->smin_value = dst_reg->umin_value;
4972− dst_reg->smax_value = dst_reg->umax_value;
4973− }
5144+ scalar_min_max_mul(dst_reg, &src_reg);
49745145 break;
49755146 case BPF_AND:
49765147 if (src_known && dst_known) {
@@ -4978,27 +5149,7 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, · 1 unmodified line
49785149 src_reg.var_off.value);
49795150 break;
49805151 }
4981− /* We get our minimum from the var_off, since that's inherently
4982− * bitwise. Our maximum is the minimum of the operands' maxima.
4983− */
4984− dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off);
4985− dst_reg->umin_value = dst_reg->var_off.value;
4986− dst_reg->umax_value = min(dst_reg->umax_value, umax_val);
4987− if (dst_reg->smin_value < 0 || smin_val < 0) {
4988− /* Lose signed bounds when ANDing negative numbers,
4989− * ain't nobody got time for that.
4990− */
4991− dst_reg->smin_value = S64_MIN;
4992− dst_reg->smax_value = S64_MAX;
4993− } else {
4994− /* ANDing two positives gives a positive, so safe to
4995− * cast result into s64.
4996− */
4997− dst_reg->smin_value = dst_reg->umin_value;
4998− dst_reg->smax_value = dst_reg->umax_value;
4999− }
5000− /* We may learn something more from the var_off */
5001− __update_reg_bounds(dst_reg);
5152+ scalar_min_max_and(dst_reg, &src_reg);
50025153 break;
50035154 case BPF_OR:
50045155 if (src_known && dst_known) {
@@ -5006,28 +5157,7 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, · 1 unmodified line
50065157 src_reg.var_off.value);
50075158 break;
50085159 }
5009− /* We get our maximum from the var_off, and our minimum is the
5010− * maximum of the operands' minima
5011− */
5012− dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off);
5013− dst_reg->umin_value = max(dst_reg->umin_value, umin_val);
5014− dst_reg->umax_value = dst_reg->var_off.value |
5015− dst_reg->var_off.mask;
5016− if (dst_reg->smin_value < 0 || smin_val < 0) {
5017− /* Lose signed bounds when ORing negative numbers,
5018− * ain't nobody got time for that.
5019− */
5020− dst_reg->smin_value = S64_MIN;
5021− dst_reg->smax_value = S64_MAX;
5022− } else {
5023− /* ORing two positives gives a positive, so safe to
5024− * cast result into s64.
5025− */
5026− dst_reg->smin_value = dst_reg->umin_value;
5027− dst_reg->smax_value = dst_reg->umax_value;
5028− }
5029− /* We may learn something more from the var_off */
5030− __update_reg_bounds(dst_reg);
5160+ scalar_min_max_or(dst_reg, &src_reg);
50315161 break;
50325162 case BPF_LSH:
50335163 if (umax_val >= insn_bitness) {
@@ -5037,22 +5167,7 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, · 3 unmodified lines
50375167 mark_reg_unknown(env, regs, insn->dst_reg);
50385168 break;
50395169 }
5040− /* We lose all sign bit information (except what we can pick
5041− * up from var_off)
5042− */
5043− dst_reg->smin_value = S64_MIN;
5044− dst_reg->smax_value = S64_MAX;
5045− /* If we might shift our top bit out, then we know nothing */
5046− if (dst_reg->umax_value > 1ULL << (63 - umax_val)) {
5047− dst_reg->umin_value = 0;
5048− dst_reg->umax_value = U64_MAX;
5049− } else {
5050− dst_reg->umin_value <<= umin_val;
5051− dst_reg->umax_value <<= umax_val;
5052− }
5053− dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val);
5054− /* We may learn something more from the var_off */
5055− __update_reg_bounds(dst_reg);
5170+ scalar_min_max_lsh(dst_reg, &src_reg);
50565171 break;
50575172 case BPF_RSH:
50585173 if (umax_val >= insn_bitness) {
@@ -5062,27 +5177,7 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, · 3 unmodified lines
50625177 mark_reg_unknown(env, regs, insn->dst_reg);
50635178 break;
50645179 }
5065− /* BPF_RSH is an unsigned shift. If the value in dst_reg might
5066− * be negative, then either:
5067− * 1) src_reg might be zero, so the sign bit of the result is
5068− * unknown, so we lose our signed bounds
5069− * 2) it's known negative, thus the unsigned bounds capture the
5070− * signed bounds
5071− * 3) the signed bounds cross zero, so they tell us nothing
5072− * about the result
5073− * If the value in dst_reg is known nonnegative, then again the
5074− * unsigned bounts capture the signed bounds.
5075− * Thus, in all cases it suffices to blow away our signed bounds
5076− * and rely on inferring new ones from the unsigned bounds and
5077− * var_off of the result.
5078− */
5079− dst_reg->smin_value = S64_MIN;
5080− dst_reg->smax_value = S64_MAX;
5081− dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val);
5082− dst_reg->umin_value >>= umax_val;
5083− dst_reg->umax_value >>= umin_val;
5084− /* We may learn something more from the var_off */
5085− __update_reg_bounds(dst_reg);
5180+ scalar_min_max_rsh(dst_reg, &src_reg);
50865181 break;
50875182 case BPF_ARSH:
50885183 if (umax_val >= insn_bitness) {
@@ -5092,27 +5187,7 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, · 3 unmodified lines
50925187 mark_reg_unknown(env, regs, insn->dst_reg);
50935188 break;
50945189 }
5095−
5096− /* Upon reaching here, src_known is true and
5097− * umax_val is equal to umin_val.
5098− */
5099− if (insn_bitness == 32) {
5100− dst_reg->smin_value = (u32)(((s32)dst_reg->smin_value) >> umin_val);
5101− dst_reg->smax_value = (u32)(((s32)dst_reg->smax_value) >> umin_val);
5102− } else {
5103− dst_reg->smin_value >>= umin_val;
5104− dst_reg->smax_value >>= umin_val;
5105− }
5106−
5107− dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val,
5108− insn_bitness);
5109−
5110− /* blow away the dst_reg umin_value/umax_value and rely on
5111− * dst_reg var_off to refine the result.
5112− */
5113− dst_reg->umin_value = 0;
5114− dst_reg->umax_value = U64_MAX;
5115− __update_reg_bounds(dst_reg);
5190+ scalar_min_max_arsh(dst_reg, &src_reg, insn_bitness);
51165191 break;
51175192 default:
51185193 mark_reg_unknown(env, regs, insn->dst_reg);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t