blob: a5b8bf8cfcfdabe0eee1df492dc62472cb46a8c6 [file] [log] [blame]
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -08001/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
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 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
11 */
12#include <linux/bpf.h>
13#include <linux/rcupdate.h>
Daniel Borkmann03e69b52015-03-14 02:27:16 +010014#include <linux/random.h>
Daniel Borkmannc04167c2015-03-14 02:27:17 +010015#include <linux/smp.h>
Daniel Borkmann17ca8cb2015-05-29 23:23:06 +020016#include <linux/ktime.h>
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -070017#include <linux/sched.h>
18#include <linux/uidgid.h>
Daniel Borkmannf3694e02016-09-09 02:45:31 +020019#include <linux/filter.h>
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080020
21/* If kernel subsystem is allowing eBPF programs to call this function,
22 * inside its own verifier_ops->get_func_proto() callback it should return
23 * bpf_map_lookup_elem_proto, so that verifier can properly check the arguments
24 *
25 * Different map implementations will rely on rcu in map methods
26 * lookup/update/delete, therefore eBPF programs must run under rcu lock
27 * if program is allowed to access maps, so check rcu_read_lock_held in
28 * all three functions.
29 */
Daniel Borkmannf3694e02016-09-09 02:45:31 +020030BPF_CALL_2(bpf_map_lookup_elem, struct bpf_map *, map, void *, key)
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080031{
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080032 WARN_ON_ONCE(!rcu_read_lock_held());
Daniel Borkmannf3694e02016-09-09 02:45:31 +020033 return (unsigned long) map->ops->map_lookup_elem(map, key);
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080034}
35
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +010036const struct bpf_func_proto bpf_map_lookup_elem_proto = {
Daniel Borkmann3324b582015-05-29 23:23:07 +020037 .func = bpf_map_lookup_elem,
38 .gpl_only = false,
39 .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
40 .arg1_type = ARG_CONST_MAP_PTR,
41 .arg2_type = ARG_PTR_TO_MAP_KEY,
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080042};
43
Daniel Borkmannf3694e02016-09-09 02:45:31 +020044BPF_CALL_4(bpf_map_update_elem, struct bpf_map *, map, void *, key,
45 void *, value, u64, flags)
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080046{
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080047 WARN_ON_ONCE(!rcu_read_lock_held());
Daniel Borkmannf3694e02016-09-09 02:45:31 +020048 return map->ops->map_update_elem(map, key, value, flags);
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080049}
50
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +010051const struct bpf_func_proto bpf_map_update_elem_proto = {
Daniel Borkmann3324b582015-05-29 23:23:07 +020052 .func = bpf_map_update_elem,
53 .gpl_only = false,
54 .ret_type = RET_INTEGER,
55 .arg1_type = ARG_CONST_MAP_PTR,
56 .arg2_type = ARG_PTR_TO_MAP_KEY,
57 .arg3_type = ARG_PTR_TO_MAP_VALUE,
58 .arg4_type = ARG_ANYTHING,
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080059};
60
Daniel Borkmannf3694e02016-09-09 02:45:31 +020061BPF_CALL_2(bpf_map_delete_elem, struct bpf_map *, map, void *, key)
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080062{
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080063 WARN_ON_ONCE(!rcu_read_lock_held());
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080064 return map->ops->map_delete_elem(map, key);
65}
66
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +010067const struct bpf_func_proto bpf_map_delete_elem_proto = {
Daniel Borkmann3324b582015-05-29 23:23:07 +020068 .func = bpf_map_delete_elem,
69 .gpl_only = false,
70 .ret_type = RET_INTEGER,
71 .arg1_type = ARG_CONST_MAP_PTR,
72 .arg2_type = ARG_PTR_TO_MAP_KEY,
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080073};
Daniel Borkmann03e69b52015-03-14 02:27:16 +010074
Daniel Borkmann03e69b52015-03-14 02:27:16 +010075const struct bpf_func_proto bpf_get_prandom_u32_proto = {
Daniel Borkmann3ad00402015-10-08 01:20:39 +020076 .func = bpf_user_rnd_u32,
Daniel Borkmann03e69b52015-03-14 02:27:16 +010077 .gpl_only = false,
78 .ret_type = RET_INTEGER,
79};
Daniel Borkmannc04167c2015-03-14 02:27:17 +010080
Daniel Borkmannf3694e02016-09-09 02:45:31 +020081BPF_CALL_0(bpf_get_smp_processor_id)
Daniel Borkmannc04167c2015-03-14 02:27:17 +010082{
Daniel Borkmann80b48c42016-06-28 12:18:26 +020083 return smp_processor_id();
Daniel Borkmannc04167c2015-03-14 02:27:17 +010084}
85
86const struct bpf_func_proto bpf_get_smp_processor_id_proto = {
87 .func = bpf_get_smp_processor_id,
88 .gpl_only = false,
89 .ret_type = RET_INTEGER,
90};
Daniel Borkmann17ca8cb2015-05-29 23:23:06 +020091
Daniel Borkmannf3694e02016-09-09 02:45:31 +020092BPF_CALL_0(bpf_ktime_get_ns)
Daniel Borkmann17ca8cb2015-05-29 23:23:06 +020093{
94 /* NMI safe access to clock monotonic */
95 return ktime_get_mono_fast_ns();
96}
97
98const struct bpf_func_proto bpf_ktime_get_ns_proto = {
99 .func = bpf_ktime_get_ns,
100 .gpl_only = true,
101 .ret_type = RET_INTEGER,
102};
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700103
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200104BPF_CALL_0(bpf_get_current_pid_tgid)
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700105{
106 struct task_struct *task = current;
107
Daniel Borkmann6088b582016-09-09 02:45:28 +0200108 if (unlikely(!task))
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700109 return -EINVAL;
110
111 return (u64) task->tgid << 32 | task->pid;
112}
113
114const struct bpf_func_proto bpf_get_current_pid_tgid_proto = {
115 .func = bpf_get_current_pid_tgid,
116 .gpl_only = false,
117 .ret_type = RET_INTEGER,
118};
119
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200120BPF_CALL_0(bpf_get_current_uid_gid)
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700121{
122 struct task_struct *task = current;
123 kuid_t uid;
124 kgid_t gid;
125
Daniel Borkmann6088b582016-09-09 02:45:28 +0200126 if (unlikely(!task))
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700127 return -EINVAL;
128
129 current_uid_gid(&uid, &gid);
130 return (u64) from_kgid(&init_user_ns, gid) << 32 |
Daniel Borkmann6088b582016-09-09 02:45:28 +0200131 from_kuid(&init_user_ns, uid);
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700132}
133
134const struct bpf_func_proto bpf_get_current_uid_gid_proto = {
135 .func = bpf_get_current_uid_gid,
136 .gpl_only = false,
137 .ret_type = RET_INTEGER,
138};
139
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200140BPF_CALL_2(bpf_get_current_comm, char *, buf, u32, size)
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700141{
142 struct task_struct *task = current;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700143
Daniel Borkmann074f528e2016-04-13 00:10:52 +0200144 if (unlikely(!task))
145 goto err_clear;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700146
Daniel Borkmann074f528e2016-04-13 00:10:52 +0200147 strncpy(buf, task->comm, size);
148
149 /* Verifier guarantees that size > 0. For task->comm exceeding
150 * size, guarantee that buf is %NUL-terminated. Unconditionally
151 * done here to save the size test.
152 */
153 buf[size - 1] = 0;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700154 return 0;
Daniel Borkmann074f528e2016-04-13 00:10:52 +0200155err_clear:
156 memset(buf, 0, size);
157 return -EINVAL;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700158}
159
160const struct bpf_func_proto bpf_get_current_comm_proto = {
161 .func = bpf_get_current_comm,
162 .gpl_only = false,
163 .ret_type = RET_INTEGER,
Daniel Borkmann074f528e2016-04-13 00:10:52 +0200164 .arg1_type = ARG_PTR_TO_RAW_STACK,
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700165 .arg2_type = ARG_CONST_STACK_SIZE,
166};