mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 02:30:34 +02:00 
			
		
		
		
	libbpf: Sanitise internal map names so they are not rejected by the kernel
The kernel only accepts map names with alphanumeric characters, underscores
and periods in their name. However, the auto-generated internal map names
used by libbpf takes their prefix from the user-supplied BPF object name,
which has no such restriction. This can lead to "Invalid argument" errors
when trying to load a BPF program using global variables.
Fix this by sanitising the map names, replacing any non-allowed characters
with underscores.
Fixes: d859900c4c ("bpf, libbpf: support global data/bss/rodata sections")
Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20200217171701.215215-1-toke@redhat.com
			
			
This commit is contained in:
		
							parent
							
								
									f25975f42f
								
							
						
					
					
						commit
						113e6b7e15
					
				
					 1 changed files with 7 additions and 1 deletions
				
			
		| 
						 | 
					@ -24,6 +24,7 @@
 | 
				
			||||||
#include <endian.h>
 | 
					#include <endian.h>
 | 
				
			||||||
#include <fcntl.h>
 | 
					#include <fcntl.h>
 | 
				
			||||||
#include <errno.h>
 | 
					#include <errno.h>
 | 
				
			||||||
 | 
					#include <ctype.h>
 | 
				
			||||||
#include <asm/unistd.h>
 | 
					#include <asm/unistd.h>
 | 
				
			||||||
#include <linux/err.h>
 | 
					#include <linux/err.h>
 | 
				
			||||||
#include <linux/kernel.h>
 | 
					#include <linux/kernel.h>
 | 
				
			||||||
| 
						 | 
					@ -1283,7 +1284,7 @@ static size_t bpf_map_mmap_sz(const struct bpf_map *map)
 | 
				
			||||||
static char *internal_map_name(struct bpf_object *obj,
 | 
					static char *internal_map_name(struct bpf_object *obj,
 | 
				
			||||||
			       enum libbpf_map_type type)
 | 
								       enum libbpf_map_type type)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	char map_name[BPF_OBJ_NAME_LEN];
 | 
						char map_name[BPF_OBJ_NAME_LEN], *p;
 | 
				
			||||||
	const char *sfx = libbpf_type_to_btf_name[type];
 | 
						const char *sfx = libbpf_type_to_btf_name[type];
 | 
				
			||||||
	int sfx_len = max((size_t)7, strlen(sfx));
 | 
						int sfx_len = max((size_t)7, strlen(sfx));
 | 
				
			||||||
	int pfx_len = min((size_t)BPF_OBJ_NAME_LEN - sfx_len - 1,
 | 
						int pfx_len = min((size_t)BPF_OBJ_NAME_LEN - sfx_len - 1,
 | 
				
			||||||
| 
						 | 
					@ -1292,6 +1293,11 @@ static char *internal_map_name(struct bpf_object *obj,
 | 
				
			||||||
	snprintf(map_name, sizeof(map_name), "%.*s%.*s", pfx_len, obj->name,
 | 
						snprintf(map_name, sizeof(map_name), "%.*s%.*s", pfx_len, obj->name,
 | 
				
			||||||
		 sfx_len, libbpf_type_to_btf_name[type]);
 | 
							 sfx_len, libbpf_type_to_btf_name[type]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* sanitise map name to characters allowed by kernel */
 | 
				
			||||||
 | 
						for (p = map_name; *p && p < map_name + sizeof(map_name); p++)
 | 
				
			||||||
 | 
							if (!isalnum(*p) && *p != '_' && *p != '.')
 | 
				
			||||||
 | 
								*p = '_';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return strdup(map_name);
 | 
						return strdup(map_name);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue