Version:  2.0.40 2.2.26 2.4.37 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16

Linux/kernel/seccomp.c

Diff markup

Differences between /kernel/seccomp.c (Version 3.16) and /kernel/seccomp.c (Version 3.6)


  1 /*                                                  1 /*
  2  * linux/kernel/seccomp.c                           2  * linux/kernel/seccomp.c
  3  *                                                  3  *
  4  * Copyright 2004-2005  Andrea Arcangeli <andr      4  * Copyright 2004-2005  Andrea Arcangeli <andrea@cpushare.com>
  5  *                                                  5  *
  6  * Copyright (C) 2012 Google, Inc.                  6  * Copyright (C) 2012 Google, Inc.
  7  * Will Drewry <wad@chromium.org>                   7  * Will Drewry <wad@chromium.org>
  8  *                                                  8  *
  9  * This defines a simple but solid secure-comp      9  * This defines a simple but solid secure-computing facility.
 10  *                                                 10  *
 11  * Mode 1 uses a fixed list of allowed system      11  * Mode 1 uses a fixed list of allowed system calls.
 12  * Mode 2 allows user-defined system call filt     12  * Mode 2 allows user-defined system call filters in the form
 13  *        of Berkeley Packet Filters/Linux Soc     13  *        of Berkeley Packet Filters/Linux Socket Filters.
 14  */                                                14  */
 15                                                    15 
 16 #include <linux/atomic.h>                          16 #include <linux/atomic.h>
 17 #include <linux/audit.h>                           17 #include <linux/audit.h>
 18 #include <linux/compat.h>                          18 #include <linux/compat.h>
 19 #include <linux/sched.h>                           19 #include <linux/sched.h>
 20 #include <linux/seccomp.h>                         20 #include <linux/seccomp.h>
 21                                                    21 
 22 /* #define SECCOMP_DEBUG 1 */                      22 /* #define SECCOMP_DEBUG 1 */
 23                                                    23 
 24 #ifdef CONFIG_SECCOMP_FILTER                       24 #ifdef CONFIG_SECCOMP_FILTER
 25 #include <asm/syscall.h>                           25 #include <asm/syscall.h>
 26 #include <linux/filter.h>                          26 #include <linux/filter.h>
 27 #include <linux/ptrace.h>                          27 #include <linux/ptrace.h>
 28 #include <linux/security.h>                        28 #include <linux/security.h>
 29 #include <linux/slab.h>                            29 #include <linux/slab.h>
 30 #include <linux/tracehook.h>                       30 #include <linux/tracehook.h>
 31 #include <linux/uaccess.h>                         31 #include <linux/uaccess.h>
 32                                                    32 
 33 /**                                                33 /**
 34  * struct seccomp_filter - container for secco     34  * struct seccomp_filter - container for seccomp BPF programs
 35  *                                                 35  *
 36  * @usage: reference count to manage the objec     36  * @usage: reference count to manage the object lifetime.
 37  *         get/put helpers should be used when     37  *         get/put helpers should be used when accessing an instance
 38  *         outside of a lifetime-guarded secti     38  *         outside of a lifetime-guarded section.  In general, this
 39  *         is only needed for handling filters     39  *         is only needed for handling filters shared across tasks.
 40  * @prev: points to a previously installed, or     40  * @prev: points to a previously installed, or inherited, filter
 41  * @len: the number of instructions in the pro     41  * @len: the number of instructions in the program
 42  * @insnsi: the BPF program instructions to ev !!  42  * @insns: the BPF program instructions to evaluate
 43  *                                                 43  *
 44  * seccomp_filter objects are organized in a t     44  * seccomp_filter objects are organized in a tree linked via the @prev
 45  * pointer.  For any task, it appears to be a      45  * pointer.  For any task, it appears to be a singly-linked list starting
 46  * with current->seccomp.filter, the most rece     46  * with current->seccomp.filter, the most recently attached or inherited filter.
 47  * However, multiple filters may share a @prev     47  * However, multiple filters may share a @prev node, by way of fork(), which
 48  * results in a unidirectional tree existing i     48  * results in a unidirectional tree existing in memory.  This is similar to
 49  * how namespaces work.                            49  * how namespaces work.
 50  *                                                 50  *
 51  * seccomp_filter objects should never be modi     51  * seccomp_filter objects should never be modified after being attached
 52  * to a task_struct (other than @usage).           52  * to a task_struct (other than @usage).
 53  */                                                53  */
 54 struct seccomp_filter {                            54 struct seccomp_filter {
 55         atomic_t usage;                            55         atomic_t usage;
 56         struct seccomp_filter *prev;               56         struct seccomp_filter *prev;
 57         struct sk_filter *prog;                !!  57         unsigned short len;  /* Instruction count */
                                                   >>  58         struct sock_filter insns[];
 58 };                                                 59 };
 59                                                    60 
 60 /* Limit any path through the tree to 256KB wo     61 /* Limit any path through the tree to 256KB worth of instructions. */
 61 #define MAX_INSNS_PER_PATH ((1 << 18) / sizeof     62 #define MAX_INSNS_PER_PATH ((1 << 18) / sizeof(struct sock_filter))
 62                                                    63 
 63 /*                                             !!  64 /**
                                                   >>  65  * get_u32 - returns a u32 offset into data
                                                   >>  66  * @data: a unsigned 64 bit value
                                                   >>  67  * @index: 0 or 1 to return the first or second 32-bits
                                                   >>  68  *
                                                   >>  69  * This inline exists to hide the length of unsigned long.  If a 32-bit
                                                   >>  70  * unsigned long is passed in, it will be extended and the top 32-bits will be
                                                   >>  71  * 0. If it is a 64-bit unsigned long, then whatever data is resident will be
                                                   >>  72  * properly returned.
                                                   >>  73  *
 64  * Endianness is explicitly ignored and left f     74  * Endianness is explicitly ignored and left for BPF program authors to manage
 65  * as per the specific architecture.               75  * as per the specific architecture.
 66  */                                                76  */
 67 static void populate_seccomp_data(struct secco !!  77 static inline u32 get_u32(u64 data, int index)
                                                   >>  78 {
                                                   >>  79         return ((u32 *)&data)[index];
                                                   >>  80 }
                                                   >>  81 
                                                   >>  82 /* Helper for bpf_load below. */
                                                   >>  83 #define BPF_DATA(_name) offsetof(struct seccomp_data, _name)
                                                   >>  84 /**
                                                   >>  85  * bpf_load: checks and returns a pointer to the requested offset
                                                   >>  86  * @off: offset into struct seccomp_data to load from
                                                   >>  87  *
                                                   >>  88  * Returns the requested 32-bits of data.
                                                   >>  89  * seccomp_check_filter() should assure that @off is 32-bit aligned
                                                   >>  90  * and not out of bounds.  Failure to do so is a BUG.
                                                   >>  91  */
                                                   >>  92 u32 seccomp_bpf_load(int off)
 68 {                                                  93 {
 69         struct task_struct *task = current;    !!  94         struct pt_regs *regs = task_pt_regs(current);
 70         struct pt_regs *regs = task_pt_regs(ta !!  95         if (off == BPF_DATA(nr))
 71         unsigned long args[6];                 !!  96                 return syscall_get_nr(current, regs);
 72                                                !!  97         if (off == BPF_DATA(arch))
 73         sd->nr = syscall_get_nr(task, regs);   !!  98                 return syscall_get_arch(current, regs);
 74         sd->arch = syscall_get_arch();         !!  99         if (off >= BPF_DATA(args[0]) && off < BPF_DATA(args[6])) {
 75         syscall_get_arguments(task, regs, 0, 6 !! 100                 unsigned long value;
 76         sd->args[0] = args[0];                 !! 101                 int arg = (off - BPF_DATA(args[0])) / sizeof(u64);
 77         sd->args[1] = args[1];                 !! 102                 int index = !!(off % sizeof(u64));
 78         sd->args[2] = args[2];                 !! 103                 syscall_get_arguments(current, regs, arg, 1, &value);
 79         sd->args[3] = args[3];                 !! 104                 return get_u32(value, index);
 80         sd->args[4] = args[4];                 !! 105         }
 81         sd->args[5] = args[5];                 !! 106         if (off == BPF_DATA(instruction_pointer))
 82         sd->instruction_pointer = KSTK_EIP(tas !! 107                 return get_u32(KSTK_EIP(current), 0);
                                                   >> 108         if (off == BPF_DATA(instruction_pointer) + sizeof(u32))
                                                   >> 109                 return get_u32(KSTK_EIP(current), 1);
                                                   >> 110         /* seccomp_check_filter should make this impossible. */
                                                   >> 111         BUG();
 83 }                                                 112 }
 84                                                   113 
 85 /**                                               114 /**
 86  *      seccomp_check_filter - verify seccomp     115  *      seccomp_check_filter - verify seccomp filter code
 87  *      @filter: filter to verify                 116  *      @filter: filter to verify
 88  *      @flen: length of filter                   117  *      @flen: length of filter
 89  *                                                118  *
 90  * Takes a previously checked filter (by sk_ch    119  * Takes a previously checked filter (by sk_chk_filter) and
 91  * redirects all filter code that loads struct    120  * redirects all filter code that loads struct sk_buff data
 92  * and related data through seccomp_bpf_load.     121  * and related data through seccomp_bpf_load.  It also
 93  * enforces length and alignment checking of t    122  * enforces length and alignment checking of those loads.
 94  *                                                123  *
 95  * Returns 0 if the rule set is legal or -EINV    124  * Returns 0 if the rule set is legal or -EINVAL if not.
 96  */                                               125  */
 97 static int seccomp_check_filter(struct sock_fi    126 static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)
 98 {                                                 127 {
 99         int pc;                                   128         int pc;
100         for (pc = 0; pc < flen; pc++) {           129         for (pc = 0; pc < flen; pc++) {
101                 struct sock_filter *ftest = &f    130                 struct sock_filter *ftest = &filter[pc];
102                 u16 code = ftest->code;           131                 u16 code = ftest->code;
103                 u32 k = ftest->k;                 132                 u32 k = ftest->k;
104                                                   133 
105                 switch (code) {                   134                 switch (code) {
106                 case BPF_LD | BPF_W | BPF_ABS: !! 135                 case BPF_S_LD_W_ABS:
107                         ftest->code = BPF_LDX  !! 136                         ftest->code = BPF_S_ANC_SECCOMP_LD_W;
108                         /* 32-bit aligned and     137                         /* 32-bit aligned and not out of bounds. */
109                         if (k >= sizeof(struct    138                         if (k >= sizeof(struct seccomp_data) || k & 3)
110                                 return -EINVAL    139                                 return -EINVAL;
111                         continue;                 140                         continue;
112                 case BPF_LD | BPF_W | BPF_LEN: !! 141                 case BPF_S_LD_W_LEN:
113                         ftest->code = BPF_LD | !! 142                         ftest->code = BPF_S_LD_IMM;
114                         ftest->k = sizeof(stru    143                         ftest->k = sizeof(struct seccomp_data);
115                         continue;                 144                         continue;
116                 case BPF_LDX | BPF_W | BPF_LEN !! 145                 case BPF_S_LDX_W_LEN:
117                         ftest->code = BPF_LDX  !! 146                         ftest->code = BPF_S_LDX_IMM;
118                         ftest->k = sizeof(stru    147                         ftest->k = sizeof(struct seccomp_data);
119                         continue;                 148                         continue;
120                 /* Explicitly include allowed     149                 /* Explicitly include allowed calls. */
121                 case BPF_RET | BPF_K:          !! 150                 case BPF_S_RET_K:
122                 case BPF_RET | BPF_A:          !! 151                 case BPF_S_RET_A:
123                 case BPF_ALU | BPF_ADD | BPF_K !! 152                 case BPF_S_ALU_ADD_K:
124                 case BPF_ALU | BPF_ADD | BPF_X !! 153                 case BPF_S_ALU_ADD_X:
125                 case BPF_ALU | BPF_SUB | BPF_K !! 154                 case BPF_S_ALU_SUB_K:
126                 case BPF_ALU | BPF_SUB | BPF_X !! 155                 case BPF_S_ALU_SUB_X:
127                 case BPF_ALU | BPF_MUL | BPF_K !! 156                 case BPF_S_ALU_MUL_K:
128                 case BPF_ALU | BPF_MUL | BPF_X !! 157                 case BPF_S_ALU_MUL_X:
129                 case BPF_ALU | BPF_DIV | BPF_K !! 158                 case BPF_S_ALU_DIV_X:
130                 case BPF_ALU | BPF_DIV | BPF_X !! 159                 case BPF_S_ALU_AND_K:
131                 case BPF_ALU | BPF_AND | BPF_K !! 160                 case BPF_S_ALU_AND_X:
132                 case BPF_ALU | BPF_AND | BPF_X !! 161                 case BPF_S_ALU_OR_K:
133                 case BPF_ALU | BPF_OR | BPF_K: !! 162                 case BPF_S_ALU_OR_X:
134                 case BPF_ALU | BPF_OR | BPF_X: !! 163                 case BPF_S_ALU_LSH_K:
135                 case BPF_ALU | BPF_XOR | BPF_K !! 164                 case BPF_S_ALU_LSH_X:
136                 case BPF_ALU | BPF_XOR | BPF_X !! 165                 case BPF_S_ALU_RSH_K:
137                 case BPF_ALU | BPF_LSH | BPF_K !! 166                 case BPF_S_ALU_RSH_X:
138                 case BPF_ALU | BPF_LSH | BPF_X !! 167                 case BPF_S_ALU_NEG:
139                 case BPF_ALU | BPF_RSH | BPF_K !! 168                 case BPF_S_LD_IMM:
140                 case BPF_ALU | BPF_RSH | BPF_X !! 169                 case BPF_S_LDX_IMM:
141                 case BPF_ALU | BPF_NEG:        !! 170                 case BPF_S_MISC_TAX:
142                 case BPF_LD | BPF_IMM:         !! 171                 case BPF_S_MISC_TXA:
143                 case BPF_LDX | BPF_IMM:        !! 172                 case BPF_S_ALU_DIV_K:
144                 case BPF_MISC | BPF_TAX:       !! 173                 case BPF_S_LD_MEM:
145                 case BPF_MISC | BPF_TXA:       !! 174                 case BPF_S_LDX_MEM:
146                 case BPF_LD | BPF_MEM:         !! 175                 case BPF_S_ST:
147                 case BPF_LDX | BPF_MEM:        !! 176                 case BPF_S_STX:
148                 case BPF_ST:                   !! 177                 case BPF_S_JMP_JA:
149                 case BPF_STX:                  !! 178                 case BPF_S_JMP_JEQ_K:
150                 case BPF_JMP | BPF_JA:         !! 179                 case BPF_S_JMP_JEQ_X:
151                 case BPF_JMP | BPF_JEQ | BPF_K !! 180                 case BPF_S_JMP_JGE_K:
152                 case BPF_JMP | BPF_JEQ | BPF_X !! 181                 case BPF_S_JMP_JGE_X:
153                 case BPF_JMP | BPF_JGE | BPF_K !! 182                 case BPF_S_JMP_JGT_K:
154                 case BPF_JMP | BPF_JGE | BPF_X !! 183                 case BPF_S_JMP_JGT_X:
155                 case BPF_JMP | BPF_JGT | BPF_K !! 184                 case BPF_S_JMP_JSET_K:
156                 case BPF_JMP | BPF_JGT | BPF_X !! 185                 case BPF_S_JMP_JSET_X:
157                 case BPF_JMP | BPF_JSET | BPF_ << 
158                 case BPF_JMP | BPF_JSET | BPF_ << 
159                         continue;                 186                         continue;
160                 default:                          187                 default:
161                         return -EINVAL;           188                         return -EINVAL;
162                 }                                 189                 }
163         }                                         190         }
164         return 0;                                 191         return 0;
165 }                                                 192 }
166                                                   193 
167 /**                                               194 /**
168  * seccomp_run_filters - evaluates all seccomp    195  * seccomp_run_filters - evaluates all seccomp filters against @syscall
169  * @syscall: number of the current system call    196  * @syscall: number of the current system call
170  *                                                197  *
171  * Returns valid seccomp BPF response codes.      198  * Returns valid seccomp BPF response codes.
172  */                                               199  */
173 static u32 seccomp_run_filters(int syscall)       200 static u32 seccomp_run_filters(int syscall)
174 {                                                 201 {
175         struct seccomp_filter *f;                 202         struct seccomp_filter *f;
176         struct seccomp_data sd;                << 
177         u32 ret = SECCOMP_RET_ALLOW;              203         u32 ret = SECCOMP_RET_ALLOW;
178                                                   204 
179         /* Ensure unexpected behavior doesn't     205         /* Ensure unexpected behavior doesn't result in failing open. */
180         if (WARN_ON(current->seccomp.filter ==    206         if (WARN_ON(current->seccomp.filter == NULL))
181                 return SECCOMP_RET_KILL;          207                 return SECCOMP_RET_KILL;
182                                                   208 
183         populate_seccomp_data(&sd);            << 
184                                                << 
185         /*                                        209         /*
186          * All filters in the list are evaluat    210          * All filters in the list are evaluated and the lowest BPF return
187          * value always takes priority (ignori    211          * value always takes priority (ignoring the DATA).
188          */                                       212          */
189         for (f = current->seccomp.filter; f; f    213         for (f = current->seccomp.filter; f; f = f->prev) {
190                 u32 cur_ret = SK_RUN_FILTER(f- !! 214                 u32 cur_ret = sk_run_filter(NULL, f->insns);
191                                                << 
192                 if ((cur_ret & SECCOMP_RET_ACT    215                 if ((cur_ret & SECCOMP_RET_ACTION) < (ret & SECCOMP_RET_ACTION))
193                         ret = cur_ret;            216                         ret = cur_ret;
194         }                                         217         }
195         return ret;                               218         return ret;
196 }                                                 219 }
197                                                   220 
198 /**                                               221 /**
199  * seccomp_attach_filter: Attaches a seccomp f    222  * seccomp_attach_filter: Attaches a seccomp filter to current.
200  * @fprog: BPF program to install                 223  * @fprog: BPF program to install
201  *                                                224  *
202  * Returns 0 on success or an errno on failure    225  * Returns 0 on success or an errno on failure.
203  */                                               226  */
204 static long seccomp_attach_filter(struct sock_    227 static long seccomp_attach_filter(struct sock_fprog *fprog)
205 {                                                 228 {
206         struct seccomp_filter *filter;            229         struct seccomp_filter *filter;
207         unsigned long fp_size = fprog->len * s    230         unsigned long fp_size = fprog->len * sizeof(struct sock_filter);
208         unsigned long total_insns = fprog->len    231         unsigned long total_insns = fprog->len;
209         struct sock_filter *fp;                << 
210         int new_len;                           << 
211         long ret;                                 232         long ret;
212                                                   233 
213         if (fprog->len == 0 || fprog->len > BP    234         if (fprog->len == 0 || fprog->len > BPF_MAXINSNS)
214                 return -EINVAL;                   235                 return -EINVAL;
215                                                   236 
216         for (filter = current->seccomp.filter;    237         for (filter = current->seccomp.filter; filter; filter = filter->prev)
217                 total_insns += filter->prog->l !! 238                 total_insns += filter->len + 4;  /* include a 4 instr penalty */
218         if (total_insns > MAX_INSNS_PER_PATH)     239         if (total_insns > MAX_INSNS_PER_PATH)
219                 return -ENOMEM;                   240                 return -ENOMEM;
220                                                   241 
221         /*                                        242         /*
222          * Installing a seccomp filter require !! 243          * Installing a seccomp filter requires that the task have
223          * CAP_SYS_ADMIN in its namespace or b    244          * CAP_SYS_ADMIN in its namespace or be running with no_new_privs.
224          * This avoids scenarios where unprivi    245          * This avoids scenarios where unprivileged tasks can affect the
225          * behavior of privileged children.       246          * behavior of privileged children.
226          */                                       247          */
227         if (!current->no_new_privs &&             248         if (!current->no_new_privs &&
228             security_capable_noaudit(current_c    249             security_capable_noaudit(current_cred(), current_user_ns(),
229                                      CAP_SYS_A    250                                      CAP_SYS_ADMIN) != 0)
230                 return -EACCES;                   251                 return -EACCES;
231                                                   252 
232         fp = kzalloc(fp_size, GFP_KERNEL|__GFP !! 253         /* Allocate a new seccomp_filter */
233         if (!fp)                               !! 254         filter = kzalloc(sizeof(struct seccomp_filter) + fp_size,
                                                   >> 255                          GFP_KERNEL|__GFP_NOWARN);
                                                   >> 256         if (!filter)
234                 return -ENOMEM;                   257                 return -ENOMEM;
                                                   >> 258         atomic_set(&filter->usage, 1);
                                                   >> 259         filter->len = fprog->len;
235                                                   260 
236         /* Copy the instructions from fprog. *    261         /* Copy the instructions from fprog. */
237         ret = -EFAULT;                            262         ret = -EFAULT;
238         if (copy_from_user(fp, fprog->filter,  !! 263         if (copy_from_user(filter->insns, fprog->filter, fp_size))
239                 goto free_prog;                !! 264                 goto fail;
240                                                   265 
241         /* Check and rewrite the fprog via the    266         /* Check and rewrite the fprog via the skb checker */
242         ret = sk_chk_filter(fp, fprog->len);   !! 267         ret = sk_chk_filter(filter->insns, filter->len);
243         if (ret)                                  268         if (ret)
244                 goto free_prog;                !! 269                 goto fail;
245                                                   270 
246         /* Check and rewrite the fprog for sec    271         /* Check and rewrite the fprog for seccomp use */
247         ret = seccomp_check_filter(fp, fprog-> !! 272         ret = seccomp_check_filter(filter->insns, filter->len);
248         if (ret)                               << 
249                 goto free_prog;                << 
250                                                << 
251         /* Convert 'sock_filter' insns to 'soc << 
252         ret = sk_convert_filter(fp, fprog->len << 
253         if (ret)                                  273         if (ret)
254                 goto free_prog;                !! 274                 goto fail;
255                                                << 
256         /* Allocate a new seccomp_filter */    << 
257         ret = -ENOMEM;                         << 
258         filter = kzalloc(sizeof(struct seccomp << 
259                          GFP_KERNEL|__GFP_NOWA << 
260         if (!filter)                           << 
261                 goto free_prog;                << 
262                                                << 
263         filter->prog = kzalloc(sk_filter_size( << 
264                                GFP_KERNEL|__GF << 
265         if (!filter->prog)                     << 
266                 goto free_filter;              << 
267                                                << 
268         ret = sk_convert_filter(fp, fprog->len << 
269         if (ret)                               << 
270                 goto free_filter_prog;         << 
271         kfree(fp);                             << 
272                                                << 
273         atomic_set(&filter->usage, 1);         << 
274         filter->prog->len = new_len;           << 
275                                                << 
276         sk_filter_select_runtime(filter->prog) << 
277                                                   275 
278         /*                                        276         /*
279          * If there is an existing filter, mak    277          * If there is an existing filter, make it the prev and don't drop its
280          * task reference.                        278          * task reference.
281          */                                       279          */
282         filter->prev = current->seccomp.filter    280         filter->prev = current->seccomp.filter;
283         current->seccomp.filter = filter;         281         current->seccomp.filter = filter;
284         return 0;                                 282         return 0;
285                                                !! 283 fail:
286 free_filter_prog:                              << 
287         kfree(filter->prog);                   << 
288 free_filter:                                   << 
289         kfree(filter);                            284         kfree(filter);
290 free_prog:                                     << 
291         kfree(fp);                             << 
292         return ret;                               285         return ret;
293 }                                                 286 }
294                                                   287 
295 /**                                               288 /**
296  * seccomp_attach_user_filter - attaches a use    289  * seccomp_attach_user_filter - attaches a user-supplied sock_fprog
297  * @user_filter: pointer to the user data cont    290  * @user_filter: pointer to the user data containing a sock_fprog.
298  *                                                291  *
299  * Returns 0 on success and non-zero otherwise    292  * Returns 0 on success and non-zero otherwise.
300  */                                               293  */
301 static long seccomp_attach_user_filter(char __ !! 294 long seccomp_attach_user_filter(char __user *user_filter)
302 {                                                 295 {
303         struct sock_fprog fprog;                  296         struct sock_fprog fprog;
304         long ret = -EFAULT;                       297         long ret = -EFAULT;
305                                                   298 
306 #ifdef CONFIG_COMPAT                              299 #ifdef CONFIG_COMPAT
307         if (is_compat_task()) {                   300         if (is_compat_task()) {
308                 struct compat_sock_fprog fprog    301                 struct compat_sock_fprog fprog32;
309                 if (copy_from_user(&fprog32, u    302                 if (copy_from_user(&fprog32, user_filter, sizeof(fprog32)))
310                         goto out;                 303                         goto out;
311                 fprog.len = fprog32.len;          304                 fprog.len = fprog32.len;
312                 fprog.filter = compat_ptr(fpro    305                 fprog.filter = compat_ptr(fprog32.filter);
313         } else /* falls through to the if belo    306         } else /* falls through to the if below. */
314 #endif                                            307 #endif
315         if (copy_from_user(&fprog, user_filter    308         if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
316                 goto out;                         309                 goto out;
317         ret = seccomp_attach_filter(&fprog);      310         ret = seccomp_attach_filter(&fprog);
318 out:                                              311 out:
319         return ret;                               312         return ret;
320 }                                                 313 }
321                                                   314 
322 /* get_seccomp_filter - increments the referen    315 /* get_seccomp_filter - increments the reference count of the filter on @tsk */
323 void get_seccomp_filter(struct task_struct *ts    316 void get_seccomp_filter(struct task_struct *tsk)
324 {                                                 317 {
325         struct seccomp_filter *orig = tsk->sec    318         struct seccomp_filter *orig = tsk->seccomp.filter;
326         if (!orig)                                319         if (!orig)
327                 return;                           320                 return;
328         /* Reference count is bounded by the n    321         /* Reference count is bounded by the number of total processes. */
329         atomic_inc(&orig->usage);                 322         atomic_inc(&orig->usage);
330 }                                                 323 }
331                                                   324 
332 /* put_seccomp_filter - decrements the ref cou    325 /* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */
333 void put_seccomp_filter(struct task_struct *ts    326 void put_seccomp_filter(struct task_struct *tsk)
334 {                                                 327 {
335         struct seccomp_filter *orig = tsk->sec    328         struct seccomp_filter *orig = tsk->seccomp.filter;
336         /* Clean up single-reference branches     329         /* Clean up single-reference branches iteratively. */
337         while (orig && atomic_dec_and_test(&or    330         while (orig && atomic_dec_and_test(&orig->usage)) {
338                 struct seccomp_filter *freeme     331                 struct seccomp_filter *freeme = orig;
339                 orig = orig->prev;                332                 orig = orig->prev;
340                 sk_filter_free(freeme->prog);  << 
341                 kfree(freeme);                    333                 kfree(freeme);
342         }                                         334         }
343 }                                                 335 }
344                                                   336 
345 /**                                               337 /**
346  * seccomp_send_sigsys - signals the task to a    338  * seccomp_send_sigsys - signals the task to allow in-process syscall emulation
347  * @syscall: syscall number to send to userlan    339  * @syscall: syscall number to send to userland
348  * @reason: filter-supplied reason code to sen    340  * @reason: filter-supplied reason code to send to userland (via si_errno)
349  *                                                341  *
350  * Forces a SIGSYS with a code of SYS_SECCOMP     342  * Forces a SIGSYS with a code of SYS_SECCOMP and related sigsys info.
351  */                                               343  */
352 static void seccomp_send_sigsys(int syscall, i    344 static void seccomp_send_sigsys(int syscall, int reason)
353 {                                                 345 {
354         struct siginfo info;                      346         struct siginfo info;
355         memset(&info, 0, sizeof(info));           347         memset(&info, 0, sizeof(info));
356         info.si_signo = SIGSYS;                   348         info.si_signo = SIGSYS;
357         info.si_code = SYS_SECCOMP;               349         info.si_code = SYS_SECCOMP;
358         info.si_call_addr = (void __user *)KST    350         info.si_call_addr = (void __user *)KSTK_EIP(current);
359         info.si_errno = reason;                   351         info.si_errno = reason;
360         info.si_arch = syscall_get_arch();     !! 352         info.si_arch = syscall_get_arch(current, task_pt_regs(current));
361         info.si_syscall = syscall;                353         info.si_syscall = syscall;
362         force_sig_info(SIGSYS, &info, current)    354         force_sig_info(SIGSYS, &info, current);
363 }                                                 355 }
364 #endif  /* CONFIG_SECCOMP_FILTER */               356 #endif  /* CONFIG_SECCOMP_FILTER */
365                                                   357 
366 /*                                                358 /*
367  * Secure computing mode 1 allows only read/wr    359  * Secure computing mode 1 allows only read/write/exit/sigreturn.
368  * To be fully secure this must be combined wi    360  * To be fully secure this must be combined with rlimit
369  * to limit the stack allocations too.            361  * to limit the stack allocations too.
370  */                                               362  */
371 static int mode1_syscalls[] = {                   363 static int mode1_syscalls[] = {
372         __NR_seccomp_read, __NR_seccomp_write,    364         __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
373         0, /* null terminated */                  365         0, /* null terminated */
374 };                                                366 };
375                                                   367 
376 #ifdef CONFIG_COMPAT                              368 #ifdef CONFIG_COMPAT
377 static int mode1_syscalls_32[] = {                369 static int mode1_syscalls_32[] = {
378         __NR_seccomp_read_32, __NR_seccomp_wri    370         __NR_seccomp_read_32, __NR_seccomp_write_32, __NR_seccomp_exit_32, __NR_seccomp_sigreturn_32,
379         0, /* null terminated */                  371         0, /* null terminated */
380 };                                                372 };
381 #endif                                            373 #endif
382                                                   374 
383 int __secure_computing(int this_syscall)          375 int __secure_computing(int this_syscall)
384 {                                                 376 {
385         int mode = current->seccomp.mode;         377         int mode = current->seccomp.mode;
386         int exit_sig = 0;                         378         int exit_sig = 0;
387         int *syscall;                             379         int *syscall;
388         u32 ret;                                  380         u32 ret;
389                                                   381 
390         switch (mode) {                           382         switch (mode) {
391         case SECCOMP_MODE_STRICT:                 383         case SECCOMP_MODE_STRICT:
392                 syscall = mode1_syscalls;         384                 syscall = mode1_syscalls;
393 #ifdef CONFIG_COMPAT                              385 #ifdef CONFIG_COMPAT
394                 if (is_compat_task())             386                 if (is_compat_task())
395                         syscall = mode1_syscal    387                         syscall = mode1_syscalls_32;
396 #endif                                            388 #endif
397                 do {                              389                 do {
398                         if (*syscall == this_s    390                         if (*syscall == this_syscall)
399                                 return 0;         391                                 return 0;
400                 } while (*++syscall);             392                 } while (*++syscall);
401                 exit_sig = SIGKILL;               393                 exit_sig = SIGKILL;
402                 ret = SECCOMP_RET_KILL;           394                 ret = SECCOMP_RET_KILL;
403                 break;                            395                 break;
404 #ifdef CONFIG_SECCOMP_FILTER                      396 #ifdef CONFIG_SECCOMP_FILTER
405         case SECCOMP_MODE_FILTER: {               397         case SECCOMP_MODE_FILTER: {
406                 int data;                         398                 int data;
407                 struct pt_regs *regs = task_pt << 
408                 ret = seccomp_run_filters(this    399                 ret = seccomp_run_filters(this_syscall);
409                 data = ret & SECCOMP_RET_DATA;    400                 data = ret & SECCOMP_RET_DATA;
410                 ret &= SECCOMP_RET_ACTION;        401                 ret &= SECCOMP_RET_ACTION;
411                 switch (ret) {                    402                 switch (ret) {
412                 case SECCOMP_RET_ERRNO:           403                 case SECCOMP_RET_ERRNO:
413                         /* Set the low-order 1    404                         /* Set the low-order 16-bits as a errno. */
414                         syscall_set_return_val !! 405                         syscall_set_return_value(current, task_pt_regs(current),
415                                                   406                                                  -data, 0);
416                         goto skip;                407                         goto skip;
417                 case SECCOMP_RET_TRAP:            408                 case SECCOMP_RET_TRAP:
418                         /* Show the handler th    409                         /* Show the handler the original registers. */
419                         syscall_rollback(curre !! 410                         syscall_rollback(current, task_pt_regs(current));
420                         /* Let the filter pass    411                         /* Let the filter pass back 16 bits of data. */
421                         seccomp_send_sigsys(th    412                         seccomp_send_sigsys(this_syscall, data);
422                         goto skip;                413                         goto skip;
423                 case SECCOMP_RET_TRACE:           414                 case SECCOMP_RET_TRACE:
424                         /* Skip these calls if    415                         /* Skip these calls if there is no tracer. */
425                         if (!ptrace_event_enab !! 416                         if (!ptrace_event_enabled(current, PTRACE_EVENT_SECCOMP))
426                                 syscall_set_re << 
427                                                << 
428                                 goto skip;        417                                 goto skip;
429                         }                      << 
430                         /* Allow the BPF to pr    418                         /* Allow the BPF to provide the event message */
431                         ptrace_event(PTRACE_EV    419                         ptrace_event(PTRACE_EVENT_SECCOMP, data);
432                         /*                        420                         /*
433                          * The delivery of a f    421                          * The delivery of a fatal signal during event
434                          * notification may si    422                          * notification may silently skip tracer notification.
435                          * Terminating the tas    423                          * Terminating the task now avoids executing a system
436                          * call that may not b    424                          * call that may not be intended.
437                          */                       425                          */
438                         if (fatal_signal_pendi    426                         if (fatal_signal_pending(current))
439                                 break;            427                                 break;
440                         if (syscall_get_nr(cur << 
441                                 goto skip;  /* << 
442                                                << 
443                         return 0;                 428                         return 0;
444                 case SECCOMP_RET_ALLOW:           429                 case SECCOMP_RET_ALLOW:
445                         return 0;                 430                         return 0;
446                 case SECCOMP_RET_KILL:            431                 case SECCOMP_RET_KILL:
447                 default:                          432                 default:
448                         break;                    433                         break;
449                 }                                 434                 }
450                 exit_sig = SIGSYS;                435                 exit_sig = SIGSYS;
451                 break;                            436                 break;
452         }                                         437         }
453 #endif                                            438 #endif
454         default:                                  439         default:
455                 BUG();                            440                 BUG();
456         }                                         441         }
457                                                   442 
458 #ifdef SECCOMP_DEBUG                              443 #ifdef SECCOMP_DEBUG
459         dump_stack();                             444         dump_stack();
460 #endif                                            445 #endif
461         audit_seccomp(this_syscall, exit_sig,     446         audit_seccomp(this_syscall, exit_sig, ret);
462         do_exit(exit_sig);                        447         do_exit(exit_sig);
463 #ifdef CONFIG_SECCOMP_FILTER                      448 #ifdef CONFIG_SECCOMP_FILTER
464 skip:                                             449 skip:
465         audit_seccomp(this_syscall, exit_sig,     450         audit_seccomp(this_syscall, exit_sig, ret);
466 #endif                                            451 #endif
467         return -1;                                452         return -1;
468 }                                                 453 }
469                                                   454 
470 long prctl_get_seccomp(void)                      455 long prctl_get_seccomp(void)
471 {                                                 456 {
472         return current->seccomp.mode;             457         return current->seccomp.mode;
473 }                                                 458 }
474                                                   459 
475 /**                                               460 /**
476  * prctl_set_seccomp: configures current->secc    461  * prctl_set_seccomp: configures current->seccomp.mode
477  * @seccomp_mode: requested mode to use           462  * @seccomp_mode: requested mode to use
478  * @filter: optional struct sock_fprog for use    463  * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
479  *                                                464  *
480  * This function may be called repeatedly with    465  * This function may be called repeatedly with a @seccomp_mode of
481  * SECCOMP_MODE_FILTER to install additional f    466  * SECCOMP_MODE_FILTER to install additional filters.  Every filter
482  * successfully installed will be evaluated (i    467  * successfully installed will be evaluated (in reverse order) for each system
483  * call the task makes.                           468  * call the task makes.
484  *                                                469  *
485  * Once current->seccomp.mode is non-zero, it     470  * Once current->seccomp.mode is non-zero, it may not be changed.
486  *                                                471  *
487  * Returns 0 on success or -EINVAL on failure.    472  * Returns 0 on success or -EINVAL on failure.
488  */                                               473  */
489 long prctl_set_seccomp(unsigned long seccomp_m    474 long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter)
490 {                                                 475 {
491         long ret = -EINVAL;                       476         long ret = -EINVAL;
492                                                   477 
493         if (current->seccomp.mode &&              478         if (current->seccomp.mode &&
494             current->seccomp.mode != seccomp_m    479             current->seccomp.mode != seccomp_mode)
495                 goto out;                         480                 goto out;
496                                                   481 
497         switch (seccomp_mode) {                   482         switch (seccomp_mode) {
498         case SECCOMP_MODE_STRICT:                 483         case SECCOMP_MODE_STRICT:
499                 ret = 0;                          484                 ret = 0;
500 #ifdef TIF_NOTSC                                  485 #ifdef TIF_NOTSC
501                 disable_TSC();                    486                 disable_TSC();
502 #endif                                            487 #endif
503                 break;                            488                 break;
504 #ifdef CONFIG_SECCOMP_FILTER                      489 #ifdef CONFIG_SECCOMP_FILTER
505         case SECCOMP_MODE_FILTER:                 490         case SECCOMP_MODE_FILTER:
506                 ret = seccomp_attach_user_filt    491                 ret = seccomp_attach_user_filter(filter);
507                 if (ret)                          492                 if (ret)
508                         goto out;                 493                         goto out;
509                 break;                            494                 break;
510 #endif                                            495 #endif
511         default:                                  496         default:
512                 goto out;                         497                 goto out;
513         }                                         498         }
514                                                   499 
515         current->seccomp.mode = seccomp_mode;     500         current->seccomp.mode = seccomp_mode;
516         set_thread_flag(TIF_SECCOMP);             501         set_thread_flag(TIF_SECCOMP);
517 out:                                              502 out:
518         return ret;                               503         return ret;
519 }                                                 504 }
520                                                   505 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us