• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2017 Facebook
2  *
3  * This program is free software; you can redistribute it and/or
4  * modify it under the terms of version 2 of the GNU General Public
5  * License as published by the Free Software Foundation.
6  */
7 #include <linux/slab.h>
8 #include <linux/bpf.h>
9 
10 #include "map_in_map.h"
11 
bpf_map_meta_alloc(int inner_map_ufd)12 struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd)
13 {
14 	struct bpf_map *inner_map, *inner_map_meta;
15 	u32 inner_map_meta_size;
16 	struct fd f;
17 
18 	f = fdget(inner_map_ufd);
19 	inner_map = __bpf_map_get(f);
20 	if (IS_ERR(inner_map))
21 		return inner_map;
22 
23 	/* prog_array->owner_prog_type and owner_jited
24 	 * is a runtime binding.  Doing static check alone
25 	 * in the verifier is not enough.
26 	 */
27 	if (inner_map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
28 		fdput(f);
29 		return ERR_PTR(-ENOTSUPP);
30 	}
31 
32 	/* Does not support >1 level map-in-map */
33 	if (inner_map->inner_map_meta) {
34 		fdput(f);
35 		return ERR_PTR(-EINVAL);
36 	}
37 
38 	inner_map_meta_size = sizeof(*inner_map_meta);
39 	/* In some cases verifier needs to access beyond just base map. */
40 	if (inner_map->ops == &array_map_ops)
41 		inner_map_meta_size = sizeof(struct bpf_array);
42 
43 	inner_map_meta = kzalloc(inner_map_meta_size, GFP_USER);
44 	if (!inner_map_meta) {
45 		fdput(f);
46 		return ERR_PTR(-ENOMEM);
47 	}
48 
49 	inner_map_meta->map_type = inner_map->map_type;
50 	inner_map_meta->key_size = inner_map->key_size;
51 	inner_map_meta->value_size = inner_map->value_size;
52 	inner_map_meta->map_flags = inner_map->map_flags;
53 	inner_map_meta->max_entries = inner_map->max_entries;
54 
55 	/* Misc members not needed in bpf_map_meta_equal() check. */
56 	inner_map_meta->ops = inner_map->ops;
57 	if (inner_map->ops == &array_map_ops) {
58 		inner_map_meta->unpriv_array = inner_map->unpriv_array;
59 		container_of(inner_map_meta, struct bpf_array, map)->index_mask =
60 		     container_of(inner_map, struct bpf_array, map)->index_mask;
61 	}
62 
63 	fdput(f);
64 	return inner_map_meta;
65 }
66 
bpf_map_meta_free(struct bpf_map * map_meta)67 void bpf_map_meta_free(struct bpf_map *map_meta)
68 {
69 	kfree(map_meta);
70 }
71 
bpf_map_meta_equal(const struct bpf_map * meta0,const struct bpf_map * meta1)72 bool bpf_map_meta_equal(const struct bpf_map *meta0,
73 			const struct bpf_map *meta1)
74 {
75 	/* No need to compare ops because it is covered by map_type */
76 	return meta0->map_type == meta1->map_type &&
77 		meta0->key_size == meta1->key_size &&
78 		meta0->value_size == meta1->value_size &&
79 		meta0->map_flags == meta1->map_flags &&
80 		meta0->max_entries == meta1->max_entries;
81 }
82 
bpf_map_fd_get_ptr(struct bpf_map * map,struct file * map_file,int ufd)83 void *bpf_map_fd_get_ptr(struct bpf_map *map,
84 			 struct file *map_file /* not used */,
85 			 int ufd)
86 {
87 	struct bpf_map *inner_map;
88 	struct fd f;
89 
90 	f = fdget(ufd);
91 	inner_map = __bpf_map_get(f);
92 	if (IS_ERR(inner_map))
93 		return inner_map;
94 
95 	if (bpf_map_meta_equal(map->inner_map_meta, inner_map))
96 		inner_map = bpf_map_inc(inner_map, false);
97 	else
98 		inner_map = ERR_PTR(-EINVAL);
99 
100 	fdput(f);
101 	return inner_map;
102 }
103 
bpf_map_fd_put_ptr(void * ptr)104 void bpf_map_fd_put_ptr(void *ptr)
105 {
106 	/* ptr->ops->map_free() has to go through one
107 	 * rcu grace period by itself.
108 	 */
109 	bpf_map_put(ptr);
110 }
111 
bpf_map_fd_sys_lookup_elem(void * ptr)112 u32 bpf_map_fd_sys_lookup_elem(void *ptr)
113 {
114 	return ((struct bpf_map *)ptr)->id;
115 }
116