Skip to contentSkip to conversation

Bpf: allow access into map value arrays

PATCH Superseded Josef Bacik 2 participants 2 messages Aug 30, 2016

Subsystems (4)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Bpf: allow access into map value arrays

Suppose you have a map array value that is somethihng like this

struct foo {
	unsigned iter;
	int array[SOME_CONSTANT];
};

You can easily insert this into an array, but you cannot modify the contents of foo->array[] after the fact. This is because we have no way to verify we won't go off the end of the array at verification time. This patch provides a start for this work. We accomplish this by keeping track of a minimum and maximum value a register could be while we're checking the code. Then at the time we try to do an access into a MAP_VALUE we verify that the maximum offset into that region is a valid access into that memory region. So in practice, code such as this

unsigned index = 0;

if (foo->iter >= SOME_CONSTANT)

	foo->iter = index;
else
	index = foo->iter++;

foo->array[index] = bar;

would be allowed, as we can verify that index will always be between 0 and SOME_CONSTANT-1.

The limitation of this patch is it only works with unsigned. With signed values the llvm compiler generates code that can't easily be adapted to this approach, so simply making unsigned index = 0 an int index = 0 will still cause the verifier to reject the program. This will be fixed in an upcoming patch.

---
 include/linux/bpf.h   |   7 ++
 kernel/bpf/verifier.c | 297 +++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 298 insertions(+), 6 deletions(-)
include/linux/bpf.h +7 -0
index 1113423..ed747f3 100644
@@ -138,6 +138,13 @@ enum bpf_reg_type {
 	 */
 	PTR_TO_PACKET,
 	PTR_TO_PACKET_END,	 /* skb->data + headlen */
+
+	/* PTR_TO_MAP_VALUE_ADJ is used for doing pointer math inside of a map
+	 * elem value.  We only allow this if we can statically verify that
+	 * access from this register are going to fall within the size of the
+	 * map element.
+	 */
+	PTR_TO_MAP_VALUE_ADJ,
 };
 
 struct bpf_prog;
kernel/bpf/verifier.c +291 -6

420 lines not on this page · raw message

Signature · 1 line
2.7.4
#1 lore raw
url: https://github.com/0day-ci/linux/commits/Josef-Bacik/Bpf-allow-access-into-map-value-arrays/20160830-213112 config: i386-randconfig-i1-201635 (attached as .config) compiler: gcc-4.8 (Debian…

Hi Josef,

[auto build test ERROR on v4.8-rc2]
[also build test ERROR on next-20160825]
[cannot apply to linus/master linux/master v4.8-rc4 v4.8-rc3]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
[Suggest to use git(>=2.9.0) format-patch --base=<commit> (or --base=auto for convenience) to record what (public, well-known) commit your patch series was built on] [Check https://git-scm.com/docs/git-format-patch for more information]

url: https://github.com/0day-ci/linux/commits/Josef-Bacik/Bpf-allow-access-into-map-value-arrays/20160830-213112
config: i386-randconfig-i1-201635 (attached as .config)
compiler: gcc-4.8 (Debian 4.8.4-1) 4.8.4 reproduce:

        # save the attached .config to linux build tree
        make ARCH=i386 

All errors (new ones prefixed by >>):

kernel/built-in.o: In function `check_alu_op':

>> verifier.c:(.text+0x8efed): undefined reference to `__udivdi3'

verifier.c:(.text+0x8f035): undefined reference to `__udivdi3'

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation