[bpf-next,v2,2/3] bpf: Sanitize LDX in jited BPF progs with KASAN

Message ID 20221125063630.536657-3-sunhao.th@gmail.com
State New
Headers
Series bpf: Add LDX/STX/ST sanitize in jited BPF progs |

Commit Message

Hao Sun Nov. 25, 2022, 6:36 a.m. UTC
  Make the verifier sanitize LDX insns in jited BPF programs. The
dst_reg and AX are free here, different insns that backup R0&R1
are inserted based on their relationships with dst_reg and src,
all the scratch regs are backed up to extended stack space before
calling the checking functions. Finally, the checking funcs are
inserted, and regs are restored.

Signed-off-by: Hao Sun <sunhao.th@gmail.com>
---
 kernel/bpf/verifier.c | 90 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 90 insertions(+)
  

Comments

kernel test robot Nov. 25, 2022, 7:40 a.m. UTC | #1
Hi Hao,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on 2b3e8f6f5b939ceeb2e097339bf78ebaaf11dfe9]

url:    https://github.com/intel-lab-lkp/linux/commits/Hao-Sun/bpf-Add-LDX-STX-ST-sanitize-in-jited-BPF-progs/20221125-143743
base:   2b3e8f6f5b939ceeb2e097339bf78ebaaf11dfe9
patch link:    https://lore.kernel.org/r/20221125063630.536657-3-sunhao.th%40gmail.com
patch subject: [PATCH bpf-next v2 2/3] bpf: Sanitize LDX in jited BPF progs with KASAN
config: powerpc-allyesconfig
compiler: powerpc-linux-gcc (GCC) 12.1.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/2a9cbd2def8de929b9d30e5cbe68ac69ddb8dcb5
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Hao-Sun/bpf-Add-LDX-STX-ST-sanitize-in-jited-BPF-progs/20221125-143743
        git checkout 2a9cbd2def8de929b9d30e5cbe68ac69ddb8dcb5
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=powerpc SHELL=/bin/bash kernel/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

   kernel/bpf/verifier.c:15335:21: warning: no previous prototype for 'bpf_asan_store8' [-Wmissing-prototypes]
   15335 |         notrace u64 bpf_asan_store##n(u##n *addr) \
         |                     ^~~~~~~~~~~~~~
   kernel/bpf/verifier.c:15342:1: note: in expansion of macro 'BPF_ASAN_STORE'
   15342 | BPF_ASAN_STORE(8);
         | ^~~~~~~~~~~~~~
   kernel/bpf/verifier.c:15335:21: warning: no previous prototype for 'bpf_asan_store16' [-Wmissing-prototypes]
   15335 |         notrace u64 bpf_asan_store##n(u##n *addr) \
         |                     ^~~~~~~~~~~~~~
   kernel/bpf/verifier.c:15343:1: note: in expansion of macro 'BPF_ASAN_STORE'
   15343 | BPF_ASAN_STORE(16);
         | ^~~~~~~~~~~~~~
   kernel/bpf/verifier.c:15335:21: warning: no previous prototype for 'bpf_asan_store32' [-Wmissing-prototypes]
   15335 |         notrace u64 bpf_asan_store##n(u##n *addr) \
         |                     ^~~~~~~~~~~~~~
   kernel/bpf/verifier.c:15344:1: note: in expansion of macro 'BPF_ASAN_STORE'
   15344 | BPF_ASAN_STORE(32);
         | ^~~~~~~~~~~~~~
   kernel/bpf/verifier.c:15335:21: warning: no previous prototype for 'bpf_asan_store64' [-Wmissing-prototypes]
   15335 |         notrace u64 bpf_asan_store##n(u##n *addr) \
         |                     ^~~~~~~~~~~~~~
   kernel/bpf/verifier.c:15345:1: note: in expansion of macro 'BPF_ASAN_STORE'
   15345 | BPF_ASAN_STORE(64);
         | ^~~~~~~~~~~~~~
>> kernel/bpf/verifier.c:15348:21: warning: no previous prototype for 'bpf_asan_load8' [-Wmissing-prototypes]
   15348 |         notrace u64 bpf_asan_load##n(u##n *addr) \
         |                     ^~~~~~~~~~~~~
   kernel/bpf/verifier.c:15353:1: note: in expansion of macro 'BPF_ASAN_LOAD'
   15353 | BPF_ASAN_LOAD(8);
         | ^~~~~~~~~~~~~
>> kernel/bpf/verifier.c:15348:21: warning: no previous prototype for 'bpf_asan_load16' [-Wmissing-prototypes]
   15348 |         notrace u64 bpf_asan_load##n(u##n *addr) \
         |                     ^~~~~~~~~~~~~
   kernel/bpf/verifier.c:15354:1: note: in expansion of macro 'BPF_ASAN_LOAD'
   15354 | BPF_ASAN_LOAD(16);
         | ^~~~~~~~~~~~~
>> kernel/bpf/verifier.c:15348:21: warning: no previous prototype for 'bpf_asan_load32' [-Wmissing-prototypes]
   15348 |         notrace u64 bpf_asan_load##n(u##n *addr) \
         |                     ^~~~~~~~~~~~~
   kernel/bpf/verifier.c:15355:1: note: in expansion of macro 'BPF_ASAN_LOAD'
   15355 | BPF_ASAN_LOAD(32);
         | ^~~~~~~~~~~~~
>> kernel/bpf/verifier.c:15348:21: warning: no previous prototype for 'bpf_asan_load64' [-Wmissing-prototypes]
   15348 |         notrace u64 bpf_asan_load##n(u##n *addr) \
         |                     ^~~~~~~~~~~~~
   kernel/bpf/verifier.c:15356:1: note: in expansion of macro 'BPF_ASAN_LOAD'
   15356 | BPF_ASAN_LOAD(64);
         | ^~~~~~~~~~~~~


vim +/bpf_asan_load8 +15348 kernel/bpf/verifier.c

 15346	
 15347	#define BPF_ASAN_LOAD(n)                         \
 15348		notrace u64 bpf_asan_load##n(u##n *addr) \
 15349		{                                        \
 15350			return *addr;                    \
 15351		}
 15352
  

Patch

diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 5519c24c5bd4..4e253fc20bf2 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -15344,6 +15344,17 @@  BPF_ASAN_STORE(16);
 BPF_ASAN_STORE(32);
 BPF_ASAN_STORE(64);
 
+#define BPF_ASAN_LOAD(n)                         \
+	notrace u64 bpf_asan_load##n(u##n *addr) \
+	{                                        \
+		return *addr;                    \
+	}
+
+BPF_ASAN_LOAD(8);
+BPF_ASAN_LOAD(16);
+BPF_ASAN_LOAD(32);
+BPF_ASAN_LOAD(64);
+
 #endif
 
 /* Do various post-verification rewrites in a single program pass.
@@ -15588,6 +15599,85 @@  static int do_misc_fixups(struct bpf_verifier_env *env)
 			insn = new_prog->insnsi + i + delta;
 			continue;
 		}
+
+		/* Sanitize LDX operation*/
+		if (BPF_CLASS(insn->code) == BPF_LDX) {
+			struct bpf_insn sanitize_fn;
+			struct bpf_insn *patch = &insn_buf[0];
+			bool dst_is_r0 = insn->dst_reg == BPF_REG_0;
+			bool dst_is_r1 = insn->dst_reg == BPF_REG_1;
+
+			if (in_patch_use_ax || insn->src_reg == BPF_REG_10)
+				continue;
+
+			switch (BPF_SIZE(insn->code)) {
+			case BPF_B:
+				sanitize_fn = BPF_EMIT_CALL(bpf_asan_load8);
+				break;
+			case BPF_H:
+				sanitize_fn = BPF_EMIT_CALL(bpf_asan_load16);
+				break;
+			case BPF_W:
+				sanitize_fn = BPF_EMIT_CALL(bpf_asan_load32);
+				break;
+			case BPF_DW:
+				sanitize_fn = BPF_EMIT_CALL(bpf_asan_load64);
+				break;
+			}
+
+			/* Backup R0 and R1, REG_AX and dst_reg are free. */
+			if (insn->src_reg == BPF_REG_1) {
+				if (!dst_is_r0)
+					*patch++ = BPF_MOV64_REG(BPF_REG_AX, BPF_REG_0);
+			} else if (insn->src_reg == BPF_REG_0) {
+				if (!dst_is_r1)
+					*patch++ = BPF_MOV64_REG(BPF_REG_AX, BPF_REG_1);
+				*patch++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_0);
+			} else if (!dst_is_r1) {
+				*patch++ = BPF_MOV64_REG(BPF_REG_AX, BPF_REG_1);
+				*patch++ = BPF_MOV64_REG(BPF_REG_1, insn->src_reg);
+				if (!dst_is_r0)
+					*patch++ = BPF_MOV64_REG(insn->dst_reg, BPF_REG_0);
+			} else {
+				*patch++ = BPF_MOV64_REG(BPF_REG_1, insn->src_reg);
+				*patch++ = BPF_MOV64_REG(BPF_REG_AX, BPF_REG_0);
+			}
+			if (insn->off != 0)
+				*patch++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, insn->off);
+			BACKUP_SCRATCH_REGS;
+			/* Invoke sanitize fn, R1~R5 are stored to stack during jit. */
+			*patch++ = sanitize_fn;
+			RESTORE_SCRATCH_REGS;
+			if (insn->off != 0)
+				*patch++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -insn->off);
+			if (insn->src_reg == BPF_REG_1) {
+				if (!dst_is_r0)
+					*patch++ = BPF_MOV64_REG(BPF_REG_0, BPF_REG_AX);
+			} else if (insn->src_reg == BPF_REG_0) {
+				*patch++ = BPF_MOV64_REG(BPF_REG_0, BPF_REG_1);
+				if (!dst_is_r1)
+					*patch++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_AX);
+			} else if (!dst_is_r1) {
+				if (!dst_is_r0)
+					*patch++ = BPF_MOV64_REG(BPF_REG_0, insn->dst_reg);
+				if (insn->src_reg == insn->dst_reg)
+					*patch++ = BPF_MOV64_REG(insn->src_reg, BPF_REG_1);
+				*patch++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_AX);
+			} else {
+				*patch++ = BPF_MOV64_REG(BPF_REG_0, BPF_REG_AX);
+			}
+			*patch++ = *insn;
+			cnt = patch - insn_buf;
+
+			new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
+			if (!new_prog)
+				return -ENOMEM;
+
+			delta += cnt - 1;
+			env->prog = prog = new_prog;
+			insn = new_prog->insnsi + i + delta;
+			continue;
+		}
 #endif
 
 		if (insn->code != (BPF_JMP | BPF_CALL))