seccomp.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/kernel/seccomp.c
  4. *
  5. * Copyright 2004-2005 Andrea Arcangeli <[email protected]>
  6. *
  7. * Copyright (C) 2012 Google, Inc.
  8. * Will Drewry <[email protected]>
  9. *
  10. * This defines a simple but solid secure-computing facility.
  11. *
  12. * Mode 1 uses a fixed list of allowed system calls.
  13. * Mode 2 allows user-defined system call filters in the form
  14. * of Berkeley Packet Filters/Linux Socket Filters.
  15. */
  16. #define pr_fmt(fmt) "seccomp: " fmt
  17. #include <linux/refcount.h>
  18. #include <linux/audit.h>
  19. #include <linux/compat.h>
  20. #include <linux/coredump.h>
  21. #include <linux/kmemleak.h>
  22. #include <linux/nospec.h>
  23. #include <linux/prctl.h>
  24. #include <linux/sched.h>
  25. #include <linux/sched/task_stack.h>
  26. #include <linux/seccomp.h>
  27. #include <linux/slab.h>
  28. #include <linux/syscalls.h>
  29. #include <linux/sysctl.h>
  30. /* Not exposed in headers: strictly internal use only. */
  31. #define SECCOMP_MODE_DEAD (SECCOMP_MODE_FILTER + 1)
  32. #ifdef CONFIG_HAVE_ARCH_SECCOMP_FILTER
  33. #include <asm/syscall.h>
  34. #endif
  35. #ifdef CONFIG_SECCOMP_FILTER
  36. #include <linux/file.h>
  37. #include <linux/filter.h>
  38. #include <linux/pid.h>
  39. #include <linux/ptrace.h>
  40. #include <linux/capability.h>
  41. #include <linux/uaccess.h>
  42. #include <linux/anon_inodes.h>
  43. #include <linux/lockdep.h>
  44. /*
  45. * When SECCOMP_IOCTL_NOTIF_ID_VALID was first introduced, it had the
  46. * wrong direction flag in the ioctl number. This is the broken one,
  47. * which the kernel needs to keep supporting until all userspaces stop
  48. * using the wrong command number.
  49. */
  50. #define SECCOMP_IOCTL_NOTIF_ID_VALID_WRONG_DIR SECCOMP_IOR(2, __u64)
  51. enum notify_state {
  52. SECCOMP_NOTIFY_INIT,
  53. SECCOMP_NOTIFY_SENT,
  54. SECCOMP_NOTIFY_REPLIED,
  55. };
  56. struct seccomp_knotif {
  57. /* The struct pid of the task whose filter triggered the notification */
  58. struct task_struct *task;
  59. /* The "cookie" for this request; this is unique for this filter. */
  60. u64 id;
  61. /*
  62. * The seccomp data. This pointer is valid the entire time this
  63. * notification is active, since it comes from __seccomp_filter which
  64. * eclipses the entire lifecycle here.
  65. */
  66. const struct seccomp_data *data;
  67. /*
  68. * Notification states. When SECCOMP_RET_USER_NOTIF is returned, a
  69. * struct seccomp_knotif is created and starts out in INIT. Once the
  70. * handler reads the notification off of an FD, it transitions to SENT.
  71. * If a signal is received the state transitions back to INIT and
  72. * another message is sent. When the userspace handler replies, state
  73. * transitions to REPLIED.
  74. */
  75. enum notify_state state;
  76. /* The return values, only valid when in SECCOMP_NOTIFY_REPLIED */
  77. int error;
  78. long val;
  79. u32 flags;
  80. /*
  81. * Signals when this has changed states, such as the listener
  82. * dying, a new seccomp addfd message, or changing to REPLIED
  83. */
  84. struct completion ready;
  85. struct list_head list;
  86. /* outstanding addfd requests */
  87. struct list_head addfd;
  88. };
  89. /**
  90. * struct seccomp_kaddfd - container for seccomp_addfd ioctl messages
  91. *
  92. * @file: A reference to the file to install in the other task
  93. * @fd: The fd number to install it at. If the fd number is -1, it means the
  94. * installing process should allocate the fd as normal.
  95. * @flags: The flags for the new file descriptor. At the moment, only O_CLOEXEC
  96. * is allowed.
  97. * @ioctl_flags: The flags used for the seccomp_addfd ioctl.
  98. * @ret: The return value of the installing process. It is set to the fd num
  99. * upon success (>= 0).
  100. * @completion: Indicates that the installing process has completed fd
  101. * installation, or gone away (either due to successful
  102. * reply, or signal)
  103. *
  104. */
  105. struct seccomp_kaddfd {
  106. struct file *file;
  107. int fd;
  108. unsigned int flags;
  109. __u32 ioctl_flags;
  110. union {
  111. bool setfd;
  112. /* To only be set on reply */
  113. int ret;
  114. };
  115. struct completion completion;
  116. struct list_head list;
  117. };
  118. /**
  119. * struct notification - container for seccomp userspace notifications. Since
  120. * most seccomp filters will not have notification listeners attached and this
  121. * structure is fairly large, we store the notification-specific stuff in a
  122. * separate structure.
  123. *
  124. * @request: A semaphore that users of this notification can wait on for
  125. * changes. Actual reads and writes are still controlled with
  126. * filter->notify_lock.
  127. * @next_id: The id of the next request.
  128. * @notifications: A list of struct seccomp_knotif elements.
  129. */
  130. struct notification {
  131. struct semaphore request;
  132. u64 next_id;
  133. struct list_head notifications;
  134. };
  135. #ifdef SECCOMP_ARCH_NATIVE
  136. /**
  137. * struct action_cache - per-filter cache of seccomp actions per
  138. * arch/syscall pair
  139. *
  140. * @allow_native: A bitmap where each bit represents whether the
  141. * filter will always allow the syscall, for the
  142. * native architecture.
  143. * @allow_compat: A bitmap where each bit represents whether the
  144. * filter will always allow the syscall, for the
  145. * compat architecture.
  146. */
  147. struct action_cache {
  148. DECLARE_BITMAP(allow_native, SECCOMP_ARCH_NATIVE_NR);
  149. #ifdef SECCOMP_ARCH_COMPAT
  150. DECLARE_BITMAP(allow_compat, SECCOMP_ARCH_COMPAT_NR);
  151. #endif
  152. };
  153. #else
  154. struct action_cache { };
  155. static inline bool seccomp_cache_check_allow(const struct seccomp_filter *sfilter,
  156. const struct seccomp_data *sd)
  157. {
  158. return false;
  159. }
  160. static inline void seccomp_cache_prepare(struct seccomp_filter *sfilter)
  161. {
  162. }
  163. #endif /* SECCOMP_ARCH_NATIVE */
  164. /**
  165. * struct seccomp_filter - container for seccomp BPF programs
  166. *
  167. * @refs: Reference count to manage the object lifetime.
  168. * A filter's reference count is incremented for each directly
  169. * attached task, once for the dependent filter, and if
  170. * requested for the user notifier. When @refs reaches zero,
  171. * the filter can be freed.
  172. * @users: A filter's @users count is incremented for each directly
  173. * attached task (filter installation, fork(), thread_sync),
  174. * and once for the dependent filter (tracked in filter->prev).
  175. * When it reaches zero it indicates that no direct or indirect
  176. * users of that filter exist. No new tasks can get associated with
  177. * this filter after reaching 0. The @users count is always smaller
  178. * or equal to @refs. Hence, reaching 0 for @users does not mean
  179. * the filter can be freed.
  180. * @cache: cache of arch/syscall mappings to actions
  181. * @log: true if all actions except for SECCOMP_RET_ALLOW should be logged
  182. * @wait_killable_recv: Put notifying process in killable state once the
  183. * notification is received by the userspace listener.
  184. * @prev: points to a previously installed, or inherited, filter
  185. * @prog: the BPF program to evaluate
  186. * @notif: the struct that holds all notification related information
  187. * @notify_lock: A lock for all notification-related accesses.
  188. * @wqh: A wait queue for poll if a notifier is in use.
  189. *
  190. * seccomp_filter objects are organized in a tree linked via the @prev
  191. * pointer. For any task, it appears to be a singly-linked list starting
  192. * with current->seccomp.filter, the most recently attached or inherited filter.
  193. * However, multiple filters may share a @prev node, by way of fork(), which
  194. * results in a unidirectional tree existing in memory. This is similar to
  195. * how namespaces work.
  196. *
  197. * seccomp_filter objects should never be modified after being attached
  198. * to a task_struct (other than @refs).
  199. */
  200. struct seccomp_filter {
  201. refcount_t refs;
  202. refcount_t users;
  203. bool log;
  204. bool wait_killable_recv;
  205. struct action_cache cache;
  206. struct seccomp_filter *prev;
  207. struct bpf_prog *prog;
  208. struct notification *notif;
  209. struct mutex notify_lock;
  210. wait_queue_head_t wqh;
  211. };
  212. /* Limit any path through the tree to 256KB worth of instructions. */
  213. #define MAX_INSNS_PER_PATH ((1 << 18) / sizeof(struct sock_filter))
  214. /*
  215. * Endianness is explicitly ignored and left for BPF program authors to manage
  216. * as per the specific architecture.
  217. */
  218. static void populate_seccomp_data(struct seccomp_data *sd)
  219. {
  220. /*
  221. * Instead of using current_pt_reg(), we're already doing the work
  222. * to safely fetch "current", so just use "task" everywhere below.
  223. */
  224. struct task_struct *task = current;
  225. struct pt_regs *regs = task_pt_regs(task);
  226. unsigned long args[6];
  227. sd->nr = syscall_get_nr(task, regs);
  228. sd->arch = syscall_get_arch(task);
  229. syscall_get_arguments(task, regs, args);
  230. sd->args[0] = args[0];
  231. sd->args[1] = args[1];
  232. sd->args[2] = args[2];
  233. sd->args[3] = args[3];
  234. sd->args[4] = args[4];
  235. sd->args[5] = args[5];
  236. sd->instruction_pointer = KSTK_EIP(task);
  237. }
  238. /**
  239. * seccomp_check_filter - verify seccomp filter code
  240. * @filter: filter to verify
  241. * @flen: length of filter
  242. *
  243. * Takes a previously checked filter (by bpf_check_classic) and
  244. * redirects all filter code that loads struct sk_buff data
  245. * and related data through seccomp_bpf_load. It also
  246. * enforces length and alignment checking of those loads.
  247. *
  248. * Returns 0 if the rule set is legal or -EINVAL if not.
  249. */
  250. static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)
  251. {
  252. int pc;
  253. for (pc = 0; pc < flen; pc++) {
  254. struct sock_filter *ftest = &filter[pc];
  255. u16 code = ftest->code;
  256. u32 k = ftest->k;
  257. switch (code) {
  258. case BPF_LD | BPF_W | BPF_ABS:
  259. ftest->code = BPF_LDX | BPF_W | BPF_ABS;
  260. /* 32-bit aligned and not out of bounds. */
  261. if (k >= sizeof(struct seccomp_data) || k & 3)
  262. return -EINVAL;
  263. continue;
  264. case BPF_LD | BPF_W | BPF_LEN:
  265. ftest->code = BPF_LD | BPF_IMM;
  266. ftest->k = sizeof(struct seccomp_data);
  267. continue;
  268. case BPF_LDX | BPF_W | BPF_LEN:
  269. ftest->code = BPF_LDX | BPF_IMM;
  270. ftest->k = sizeof(struct seccomp_data);
  271. continue;
  272. /* Explicitly include allowed calls. */
  273. case BPF_RET | BPF_K:
  274. case BPF_RET | BPF_A:
  275. case BPF_ALU | BPF_ADD | BPF_K:
  276. case BPF_ALU | BPF_ADD | BPF_X:
  277. case BPF_ALU | BPF_SUB | BPF_K:
  278. case BPF_ALU | BPF_SUB | BPF_X:
  279. case BPF_ALU | BPF_MUL | BPF_K:
  280. case BPF_ALU | BPF_MUL | BPF_X:
  281. case BPF_ALU | BPF_DIV | BPF_K:
  282. case BPF_ALU | BPF_DIV | BPF_X:
  283. case BPF_ALU | BPF_AND | BPF_K:
  284. case BPF_ALU | BPF_AND | BPF_X:
  285. case BPF_ALU | BPF_OR | BPF_K:
  286. case BPF_ALU | BPF_OR | BPF_X:
  287. case BPF_ALU | BPF_XOR | BPF_K:
  288. case BPF_ALU | BPF_XOR | BPF_X:
  289. case BPF_ALU | BPF_LSH | BPF_K:
  290. case BPF_ALU | BPF_LSH | BPF_X:
  291. case BPF_ALU | BPF_RSH | BPF_K:
  292. case BPF_ALU | BPF_RSH | BPF_X:
  293. case BPF_ALU | BPF_NEG:
  294. case BPF_LD | BPF_IMM:
  295. case BPF_LDX | BPF_IMM:
  296. case BPF_MISC | BPF_TAX:
  297. case BPF_MISC | BPF_TXA:
  298. case BPF_LD | BPF_MEM:
  299. case BPF_LDX | BPF_MEM:
  300. case BPF_ST:
  301. case BPF_STX:
  302. case BPF_JMP | BPF_JA:
  303. case BPF_JMP | BPF_JEQ | BPF_K:
  304. case BPF_JMP | BPF_JEQ | BPF_X:
  305. case BPF_JMP | BPF_JGE | BPF_K:
  306. case BPF_JMP | BPF_JGE | BPF_X:
  307. case BPF_JMP | BPF_JGT | BPF_K:
  308. case BPF_JMP | BPF_JGT | BPF_X:
  309. case BPF_JMP | BPF_JSET | BPF_K:
  310. case BPF_JMP | BPF_JSET | BPF_X:
  311. continue;
  312. default:
  313. return -EINVAL;
  314. }
  315. }
  316. return 0;
  317. }
  318. #ifdef SECCOMP_ARCH_NATIVE
  319. static inline bool seccomp_cache_check_allow_bitmap(const void *bitmap,
  320. size_t bitmap_size,
  321. int syscall_nr)
  322. {
  323. if (unlikely(syscall_nr < 0 || syscall_nr >= bitmap_size))
  324. return false;
  325. syscall_nr = array_index_nospec(syscall_nr, bitmap_size);
  326. return test_bit(syscall_nr, bitmap);
  327. }
  328. /**
  329. * seccomp_cache_check_allow - lookup seccomp cache
  330. * @sfilter: The seccomp filter
  331. * @sd: The seccomp data to lookup the cache with
  332. *
  333. * Returns true if the seccomp_data is cached and allowed.
  334. */
  335. static inline bool seccomp_cache_check_allow(const struct seccomp_filter *sfilter,
  336. const struct seccomp_data *sd)
  337. {
  338. int syscall_nr = sd->nr;
  339. const struct action_cache *cache = &sfilter->cache;
  340. #ifndef SECCOMP_ARCH_COMPAT
  341. /* A native-only architecture doesn't need to check sd->arch. */
  342. return seccomp_cache_check_allow_bitmap(cache->allow_native,
  343. SECCOMP_ARCH_NATIVE_NR,
  344. syscall_nr);
  345. #else
  346. if (likely(sd->arch == SECCOMP_ARCH_NATIVE))
  347. return seccomp_cache_check_allow_bitmap(cache->allow_native,
  348. SECCOMP_ARCH_NATIVE_NR,
  349. syscall_nr);
  350. if (likely(sd->arch == SECCOMP_ARCH_COMPAT))
  351. return seccomp_cache_check_allow_bitmap(cache->allow_compat,
  352. SECCOMP_ARCH_COMPAT_NR,
  353. syscall_nr);
  354. #endif /* SECCOMP_ARCH_COMPAT */
  355. WARN_ON_ONCE(true);
  356. return false;
  357. }
  358. #endif /* SECCOMP_ARCH_NATIVE */
  359. /**
  360. * seccomp_run_filters - evaluates all seccomp filters against @sd
  361. * @sd: optional seccomp data to be passed to filters
  362. * @match: stores struct seccomp_filter that resulted in the return value,
  363. * unless filter returned SECCOMP_RET_ALLOW, in which case it will
  364. * be unchanged.
  365. *
  366. * Returns valid seccomp BPF response codes.
  367. */
  368. #define ACTION_ONLY(ret) ((s32)((ret) & (SECCOMP_RET_ACTION_FULL)))
  369. static u32 seccomp_run_filters(const struct seccomp_data *sd,
  370. struct seccomp_filter **match)
  371. {
  372. u32 ret = SECCOMP_RET_ALLOW;
  373. /* Make sure cross-thread synced filter points somewhere sane. */
  374. struct seccomp_filter *f =
  375. READ_ONCE(current->seccomp.filter);
  376. /* Ensure unexpected behavior doesn't result in failing open. */
  377. if (WARN_ON(f == NULL))
  378. return SECCOMP_RET_KILL_PROCESS;
  379. if (seccomp_cache_check_allow(f, sd))
  380. return SECCOMP_RET_ALLOW;
  381. /*
  382. * All filters in the list are evaluated and the lowest BPF return
  383. * value always takes priority (ignoring the DATA).
  384. */
  385. for (; f; f = f->prev) {
  386. u32 cur_ret = bpf_prog_run_pin_on_cpu(f->prog, sd);
  387. if (ACTION_ONLY(cur_ret) < ACTION_ONLY(ret)) {
  388. ret = cur_ret;
  389. *match = f;
  390. }
  391. }
  392. return ret;
  393. }
  394. #endif /* CONFIG_SECCOMP_FILTER */
  395. static inline bool seccomp_may_assign_mode(unsigned long seccomp_mode)
  396. {
  397. assert_spin_locked(&current->sighand->siglock);
  398. if (current->seccomp.mode && current->seccomp.mode != seccomp_mode)
  399. return false;
  400. return true;
  401. }
  402. void __weak arch_seccomp_spec_mitigate(struct task_struct *task) { }
  403. static inline void seccomp_assign_mode(struct task_struct *task,
  404. unsigned long seccomp_mode,
  405. unsigned long flags)
  406. {
  407. assert_spin_locked(&task->sighand->siglock);
  408. task->seccomp.mode = seccomp_mode;
  409. /*
  410. * Make sure SYSCALL_WORK_SECCOMP cannot be set before the mode (and
  411. * filter) is set.
  412. */
  413. smp_mb__before_atomic();
  414. /* Assume default seccomp processes want spec flaw mitigation. */
  415. if ((flags & SECCOMP_FILTER_FLAG_SPEC_ALLOW) == 0)
  416. arch_seccomp_spec_mitigate(task);
  417. set_task_syscall_work(task, SECCOMP);
  418. }
  419. #ifdef CONFIG_SECCOMP_FILTER
  420. /* Returns 1 if the parent is an ancestor of the child. */
  421. static int is_ancestor(struct seccomp_filter *parent,
  422. struct seccomp_filter *child)
  423. {
  424. /* NULL is the root ancestor. */
  425. if (parent == NULL)
  426. return 1;
  427. for (; child; child = child->prev)
  428. if (child == parent)
  429. return 1;
  430. return 0;
  431. }
  432. /**
  433. * seccomp_can_sync_threads: checks if all threads can be synchronized
  434. *
  435. * Expects sighand and cred_guard_mutex locks to be held.
  436. *
  437. * Returns 0 on success, -ve on error, or the pid of a thread which was
  438. * either not in the correct seccomp mode or did not have an ancestral
  439. * seccomp filter.
  440. */
  441. static inline pid_t seccomp_can_sync_threads(void)
  442. {
  443. struct task_struct *thread, *caller;
  444. BUG_ON(!mutex_is_locked(&current->signal->cred_guard_mutex));
  445. assert_spin_locked(&current->sighand->siglock);
  446. /* Validate all threads being eligible for synchronization. */
  447. caller = current;
  448. for_each_thread(caller, thread) {
  449. pid_t failed;
  450. /* Skip current, since it is initiating the sync. */
  451. if (thread == caller)
  452. continue;
  453. if (thread->seccomp.mode == SECCOMP_MODE_DISABLED ||
  454. (thread->seccomp.mode == SECCOMP_MODE_FILTER &&
  455. is_ancestor(thread->seccomp.filter,
  456. caller->seccomp.filter)))
  457. continue;
  458. /* Return the first thread that cannot be synchronized. */
  459. failed = task_pid_vnr(thread);
  460. /* If the pid cannot be resolved, then return -ESRCH */
  461. if (WARN_ON(failed == 0))
  462. failed = -ESRCH;
  463. return failed;
  464. }
  465. return 0;
  466. }
  467. static inline void seccomp_filter_free(struct seccomp_filter *filter)
  468. {
  469. if (filter) {
  470. bpf_prog_destroy(filter->prog);
  471. kfree(filter);
  472. }
  473. }
  474. static void __seccomp_filter_orphan(struct seccomp_filter *orig)
  475. {
  476. while (orig && refcount_dec_and_test(&orig->users)) {
  477. if (waitqueue_active(&orig->wqh))
  478. wake_up_poll(&orig->wqh, EPOLLHUP);
  479. orig = orig->prev;
  480. }
  481. }
  482. static void __put_seccomp_filter(struct seccomp_filter *orig)
  483. {
  484. /* Clean up single-reference branches iteratively. */
  485. while (orig && refcount_dec_and_test(&orig->refs)) {
  486. struct seccomp_filter *freeme = orig;
  487. orig = orig->prev;
  488. seccomp_filter_free(freeme);
  489. }
  490. }
  491. static void __seccomp_filter_release(struct seccomp_filter *orig)
  492. {
  493. /* Notify about any unused filters in the task's former filter tree. */
  494. __seccomp_filter_orphan(orig);
  495. /* Finally drop all references to the task's former tree. */
  496. __put_seccomp_filter(orig);
  497. }
  498. /**
  499. * seccomp_filter_release - Detach the task from its filter tree,
  500. * drop its reference count, and notify
  501. * about unused filters
  502. *
  503. * This function should only be called when the task is exiting as
  504. * it detaches it from its filter tree. As such, READ_ONCE() and
  505. * barriers are not needed here, as would normally be needed.
  506. */
  507. void seccomp_filter_release(struct task_struct *tsk)
  508. {
  509. struct seccomp_filter *orig = tsk->seccomp.filter;
  510. /* We are effectively holding the siglock by not having any sighand. */
  511. WARN_ON(tsk->sighand != NULL);
  512. /* Detach task from its filter tree. */
  513. tsk->seccomp.filter = NULL;
  514. __seccomp_filter_release(orig);
  515. }
  516. /**
  517. * seccomp_sync_threads: sets all threads to use current's filter
  518. *
  519. * Expects sighand and cred_guard_mutex locks to be held, and for
  520. * seccomp_can_sync_threads() to have returned success already
  521. * without dropping the locks.
  522. *
  523. */
  524. static inline void seccomp_sync_threads(unsigned long flags)
  525. {
  526. struct task_struct *thread, *caller;
  527. BUG_ON(!mutex_is_locked(&current->signal->cred_guard_mutex));
  528. assert_spin_locked(&current->sighand->siglock);
  529. /* Synchronize all threads. */
  530. caller = current;
  531. for_each_thread(caller, thread) {
  532. /* Skip current, since it needs no changes. */
  533. if (thread == caller)
  534. continue;
  535. /* Get a task reference for the new leaf node. */
  536. get_seccomp_filter(caller);
  537. /*
  538. * Drop the task reference to the shared ancestor since
  539. * current's path will hold a reference. (This also
  540. * allows a put before the assignment.)
  541. */
  542. __seccomp_filter_release(thread->seccomp.filter);
  543. /* Make our new filter tree visible. */
  544. smp_store_release(&thread->seccomp.filter,
  545. caller->seccomp.filter);
  546. atomic_set(&thread->seccomp.filter_count,
  547. atomic_read(&caller->seccomp.filter_count));
  548. /*
  549. * Don't let an unprivileged task work around
  550. * the no_new_privs restriction by creating
  551. * a thread that sets it up, enters seccomp,
  552. * then dies.
  553. */
  554. if (task_no_new_privs(caller))
  555. task_set_no_new_privs(thread);
  556. /*
  557. * Opt the other thread into seccomp if needed.
  558. * As threads are considered to be trust-realm
  559. * equivalent (see ptrace_may_access), it is safe to
  560. * allow one thread to transition the other.
  561. */
  562. if (thread->seccomp.mode == SECCOMP_MODE_DISABLED)
  563. seccomp_assign_mode(thread, SECCOMP_MODE_FILTER,
  564. flags);
  565. }
  566. }
  567. /**
  568. * seccomp_prepare_filter: Prepares a seccomp filter for use.
  569. * @fprog: BPF program to install
  570. *
  571. * Returns filter on success or an ERR_PTR on failure.
  572. */
  573. static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog)
  574. {
  575. struct seccomp_filter *sfilter;
  576. int ret;
  577. const bool save_orig =
  578. #if defined(CONFIG_CHECKPOINT_RESTORE) || defined(SECCOMP_ARCH_NATIVE)
  579. true;
  580. #else
  581. false;
  582. #endif
  583. if (fprog->len == 0 || fprog->len > BPF_MAXINSNS)
  584. return ERR_PTR(-EINVAL);
  585. BUG_ON(INT_MAX / fprog->len < sizeof(struct sock_filter));
  586. /*
  587. * Installing a seccomp filter requires that the task has
  588. * CAP_SYS_ADMIN in its namespace or be running with no_new_privs.
  589. * This avoids scenarios where unprivileged tasks can affect the
  590. * behavior of privileged children.
  591. */
  592. if (!task_no_new_privs(current) &&
  593. !ns_capable_noaudit(current_user_ns(), CAP_SYS_ADMIN))
  594. return ERR_PTR(-EACCES);
  595. /* Allocate a new seccomp_filter */
  596. sfilter = kzalloc(sizeof(*sfilter), GFP_KERNEL | __GFP_NOWARN);
  597. if (!sfilter)
  598. return ERR_PTR(-ENOMEM);
  599. mutex_init(&sfilter->notify_lock);
  600. ret = bpf_prog_create_from_user(&sfilter->prog, fprog,
  601. seccomp_check_filter, save_orig);
  602. if (ret < 0) {
  603. kfree(sfilter);
  604. return ERR_PTR(ret);
  605. }
  606. refcount_set(&sfilter->refs, 1);
  607. refcount_set(&sfilter->users, 1);
  608. init_waitqueue_head(&sfilter->wqh);
  609. return sfilter;
  610. }
  611. /**
  612. * seccomp_prepare_user_filter - prepares a user-supplied sock_fprog
  613. * @user_filter: pointer to the user data containing a sock_fprog.
  614. *
  615. * Returns 0 on success and non-zero otherwise.
  616. */
  617. static struct seccomp_filter *
  618. seccomp_prepare_user_filter(const char __user *user_filter)
  619. {
  620. struct sock_fprog fprog;
  621. struct seccomp_filter *filter = ERR_PTR(-EFAULT);
  622. #ifdef CONFIG_COMPAT
  623. if (in_compat_syscall()) {
  624. struct compat_sock_fprog fprog32;
  625. if (copy_from_user(&fprog32, user_filter, sizeof(fprog32)))
  626. goto out;
  627. fprog.len = fprog32.len;
  628. fprog.filter = compat_ptr(fprog32.filter);
  629. } else /* falls through to the if below. */
  630. #endif
  631. if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
  632. goto out;
  633. filter = seccomp_prepare_filter(&fprog);
  634. out:
  635. return filter;
  636. }
  637. #ifdef SECCOMP_ARCH_NATIVE
  638. /**
  639. * seccomp_is_const_allow - check if filter is constant allow with given data
  640. * @fprog: The BPF programs
  641. * @sd: The seccomp data to check against, only syscall number and arch
  642. * number are considered constant.
  643. */
  644. static bool seccomp_is_const_allow(struct sock_fprog_kern *fprog,
  645. struct seccomp_data *sd)
  646. {
  647. unsigned int reg_value = 0;
  648. unsigned int pc;
  649. bool op_res;
  650. if (WARN_ON_ONCE(!fprog))
  651. return false;
  652. for (pc = 0; pc < fprog->len; pc++) {
  653. struct sock_filter *insn = &fprog->filter[pc];
  654. u16 code = insn->code;
  655. u32 k = insn->k;
  656. switch (code) {
  657. case BPF_LD | BPF_W | BPF_ABS:
  658. switch (k) {
  659. case offsetof(struct seccomp_data, nr):
  660. reg_value = sd->nr;
  661. break;
  662. case offsetof(struct seccomp_data, arch):
  663. reg_value = sd->arch;
  664. break;
  665. default:
  666. /* can't optimize (non-constant value load) */
  667. return false;
  668. }
  669. break;
  670. case BPF_RET | BPF_K:
  671. /* reached return with constant values only, check allow */
  672. return k == SECCOMP_RET_ALLOW;
  673. case BPF_JMP | BPF_JA:
  674. pc += insn->k;
  675. break;
  676. case BPF_JMP | BPF_JEQ | BPF_K:
  677. case BPF_JMP | BPF_JGE | BPF_K:
  678. case BPF_JMP | BPF_JGT | BPF_K:
  679. case BPF_JMP | BPF_JSET | BPF_K:
  680. switch (BPF_OP(code)) {
  681. case BPF_JEQ:
  682. op_res = reg_value == k;
  683. break;
  684. case BPF_JGE:
  685. op_res = reg_value >= k;
  686. break;
  687. case BPF_JGT:
  688. op_res = reg_value > k;
  689. break;
  690. case BPF_JSET:
  691. op_res = !!(reg_value & k);
  692. break;
  693. default:
  694. /* can't optimize (unknown jump) */
  695. return false;
  696. }
  697. pc += op_res ? insn->jt : insn->jf;
  698. break;
  699. case BPF_ALU | BPF_AND | BPF_K:
  700. reg_value &= k;
  701. break;
  702. default:
  703. /* can't optimize (unknown insn) */
  704. return false;
  705. }
  706. }
  707. /* ran off the end of the filter?! */
  708. WARN_ON(1);
  709. return false;
  710. }
  711. static void seccomp_cache_prepare_bitmap(struct seccomp_filter *sfilter,
  712. void *bitmap, const void *bitmap_prev,
  713. size_t bitmap_size, int arch)
  714. {
  715. struct sock_fprog_kern *fprog = sfilter->prog->orig_prog;
  716. struct seccomp_data sd;
  717. int nr;
  718. if (bitmap_prev) {
  719. /* The new filter must be as restrictive as the last. */
  720. bitmap_copy(bitmap, bitmap_prev, bitmap_size);
  721. } else {
  722. /* Before any filters, all syscalls are always allowed. */
  723. bitmap_fill(bitmap, bitmap_size);
  724. }
  725. for (nr = 0; nr < bitmap_size; nr++) {
  726. /* No bitmap change: not a cacheable action. */
  727. if (!test_bit(nr, bitmap))
  728. continue;
  729. sd.nr = nr;
  730. sd.arch = arch;
  731. /* No bitmap change: continue to always allow. */
  732. if (seccomp_is_const_allow(fprog, &sd))
  733. continue;
  734. /*
  735. * Not a cacheable action: always run filters.
  736. * atomic clear_bit() not needed, filter not visible yet.
  737. */
  738. __clear_bit(nr, bitmap);
  739. }
  740. }
  741. /**
  742. * seccomp_cache_prepare - emulate the filter to find cacheable syscalls
  743. * @sfilter: The seccomp filter
  744. *
  745. * Returns 0 if successful or -errno if error occurred.
  746. */
  747. static void seccomp_cache_prepare(struct seccomp_filter *sfilter)
  748. {
  749. struct action_cache *cache = &sfilter->cache;
  750. const struct action_cache *cache_prev =
  751. sfilter->prev ? &sfilter->prev->cache : NULL;
  752. seccomp_cache_prepare_bitmap(sfilter, cache->allow_native,
  753. cache_prev ? cache_prev->allow_native : NULL,
  754. SECCOMP_ARCH_NATIVE_NR,
  755. SECCOMP_ARCH_NATIVE);
  756. #ifdef SECCOMP_ARCH_COMPAT
  757. seccomp_cache_prepare_bitmap(sfilter, cache->allow_compat,
  758. cache_prev ? cache_prev->allow_compat : NULL,
  759. SECCOMP_ARCH_COMPAT_NR,
  760. SECCOMP_ARCH_COMPAT);
  761. #endif /* SECCOMP_ARCH_COMPAT */
  762. }
  763. #endif /* SECCOMP_ARCH_NATIVE */
  764. /**
  765. * seccomp_attach_filter: validate and attach filter
  766. * @flags: flags to change filter behavior
  767. * @filter: seccomp filter to add to the current process
  768. *
  769. * Caller must be holding current->sighand->siglock lock.
  770. *
  771. * Returns 0 on success, -ve on error, or
  772. * - in TSYNC mode: the pid of a thread which was either not in the correct
  773. * seccomp mode or did not have an ancestral seccomp filter
  774. * - in NEW_LISTENER mode: the fd of the new listener
  775. */
  776. static long seccomp_attach_filter(unsigned int flags,
  777. struct seccomp_filter *filter)
  778. {
  779. unsigned long total_insns;
  780. struct seccomp_filter *walker;
  781. assert_spin_locked(&current->sighand->siglock);
  782. /* Validate resulting filter length. */
  783. total_insns = filter->prog->len;
  784. for (walker = current->seccomp.filter; walker; walker = walker->prev)
  785. total_insns += walker->prog->len + 4; /* 4 instr penalty */
  786. if (total_insns > MAX_INSNS_PER_PATH)
  787. return -ENOMEM;
  788. /* If thread sync has been requested, check that it is possible. */
  789. if (flags & SECCOMP_FILTER_FLAG_TSYNC) {
  790. int ret;
  791. ret = seccomp_can_sync_threads();
  792. if (ret) {
  793. if (flags & SECCOMP_FILTER_FLAG_TSYNC_ESRCH)
  794. return -ESRCH;
  795. else
  796. return ret;
  797. }
  798. }
  799. /* Set log flag, if present. */
  800. if (flags & SECCOMP_FILTER_FLAG_LOG)
  801. filter->log = true;
  802. /* Set wait killable flag, if present. */
  803. if (flags & SECCOMP_FILTER_FLAG_WAIT_KILLABLE_RECV)
  804. filter->wait_killable_recv = true;
  805. /*
  806. * If there is an existing filter, make it the prev and don't drop its
  807. * task reference.
  808. */
  809. filter->prev = current->seccomp.filter;
  810. seccomp_cache_prepare(filter);
  811. current->seccomp.filter = filter;
  812. atomic_inc(&current->seccomp.filter_count);
  813. /* Now that the new filter is in place, synchronize to all threads. */
  814. if (flags & SECCOMP_FILTER_FLAG_TSYNC)
  815. seccomp_sync_threads(flags);
  816. return 0;
  817. }
  818. static void __get_seccomp_filter(struct seccomp_filter *filter)
  819. {
  820. refcount_inc(&filter->refs);
  821. }
  822. /* get_seccomp_filter - increments the reference count of the filter on @tsk */
  823. void get_seccomp_filter(struct task_struct *tsk)
  824. {
  825. struct seccomp_filter *orig = tsk->seccomp.filter;
  826. if (!orig)
  827. return;
  828. __get_seccomp_filter(orig);
  829. refcount_inc(&orig->users);
  830. }
  831. #endif /* CONFIG_SECCOMP_FILTER */
  832. /* For use with seccomp_actions_logged */
  833. #define SECCOMP_LOG_KILL_PROCESS (1 << 0)
  834. #define SECCOMP_LOG_KILL_THREAD (1 << 1)
  835. #define SECCOMP_LOG_TRAP (1 << 2)
  836. #define SECCOMP_LOG_ERRNO (1 << 3)
  837. #define SECCOMP_LOG_TRACE (1 << 4)
  838. #define SECCOMP_LOG_LOG (1 << 5)
  839. #define SECCOMP_LOG_ALLOW (1 << 6)
  840. #define SECCOMP_LOG_USER_NOTIF (1 << 7)
  841. static u32 seccomp_actions_logged = SECCOMP_LOG_KILL_PROCESS |
  842. SECCOMP_LOG_KILL_THREAD |
  843. SECCOMP_LOG_TRAP |
  844. SECCOMP_LOG_ERRNO |
  845. SECCOMP_LOG_USER_NOTIF |
  846. SECCOMP_LOG_TRACE |
  847. SECCOMP_LOG_LOG;
  848. static inline void seccomp_log(unsigned long syscall, long signr, u32 action,
  849. bool requested)
  850. {
  851. bool log = false;
  852. switch (action) {
  853. case SECCOMP_RET_ALLOW:
  854. break;
  855. case SECCOMP_RET_TRAP:
  856. log = requested && seccomp_actions_logged & SECCOMP_LOG_TRAP;
  857. break;
  858. case SECCOMP_RET_ERRNO:
  859. log = requested && seccomp_actions_logged & SECCOMP_LOG_ERRNO;
  860. break;
  861. case SECCOMP_RET_TRACE:
  862. log = requested && seccomp_actions_logged & SECCOMP_LOG_TRACE;
  863. break;
  864. case SECCOMP_RET_USER_NOTIF:
  865. log = requested && seccomp_actions_logged & SECCOMP_LOG_USER_NOTIF;
  866. break;
  867. case SECCOMP_RET_LOG:
  868. log = seccomp_actions_logged & SECCOMP_LOG_LOG;
  869. break;
  870. case SECCOMP_RET_KILL_THREAD:
  871. log = seccomp_actions_logged & SECCOMP_LOG_KILL_THREAD;
  872. break;
  873. case SECCOMP_RET_KILL_PROCESS:
  874. default:
  875. log = seccomp_actions_logged & SECCOMP_LOG_KILL_PROCESS;
  876. }
  877. /*
  878. * Emit an audit message when the action is RET_KILL_*, RET_LOG, or the
  879. * FILTER_FLAG_LOG bit was set. The admin has the ability to silence
  880. * any action from being logged by removing the action name from the
  881. * seccomp_actions_logged sysctl.
  882. */
  883. if (!log)
  884. return;
  885. audit_seccomp(syscall, signr, action);
  886. }
  887. /*
  888. * Secure computing mode 1 allows only read/write/exit/sigreturn.
  889. * To be fully secure this must be combined with rlimit
  890. * to limit the stack allocations too.
  891. */
  892. static const int mode1_syscalls[] = {
  893. __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
  894. -1, /* negative terminated */
  895. };
  896. static void __secure_computing_strict(int this_syscall)
  897. {
  898. const int *allowed_syscalls = mode1_syscalls;
  899. #ifdef CONFIG_COMPAT
  900. if (in_compat_syscall())
  901. allowed_syscalls = get_compat_mode1_syscalls();
  902. #endif
  903. do {
  904. if (*allowed_syscalls == this_syscall)
  905. return;
  906. } while (*++allowed_syscalls != -1);
  907. #ifdef SECCOMP_DEBUG
  908. dump_stack();
  909. #endif
  910. current->seccomp.mode = SECCOMP_MODE_DEAD;
  911. seccomp_log(this_syscall, SIGKILL, SECCOMP_RET_KILL_THREAD, true);
  912. do_exit(SIGKILL);
  913. }
  914. #ifndef CONFIG_HAVE_ARCH_SECCOMP_FILTER
  915. void secure_computing_strict(int this_syscall)
  916. {
  917. int mode = current->seccomp.mode;
  918. if (IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) &&
  919. unlikely(current->ptrace & PT_SUSPEND_SECCOMP))
  920. return;
  921. if (mode == SECCOMP_MODE_DISABLED)
  922. return;
  923. else if (mode == SECCOMP_MODE_STRICT)
  924. __secure_computing_strict(this_syscall);
  925. else
  926. BUG();
  927. }
  928. #else
  929. #ifdef CONFIG_SECCOMP_FILTER
  930. static u64 seccomp_next_notify_id(struct seccomp_filter *filter)
  931. {
  932. /*
  933. * Note: overflow is ok here, the id just needs to be unique per
  934. * filter.
  935. */
  936. lockdep_assert_held(&filter->notify_lock);
  937. return filter->notif->next_id++;
  938. }
  939. static void seccomp_handle_addfd(struct seccomp_kaddfd *addfd, struct seccomp_knotif *n)
  940. {
  941. int fd;
  942. /*
  943. * Remove the notification, and reset the list pointers, indicating
  944. * that it has been handled.
  945. */
  946. list_del_init(&addfd->list);
  947. if (!addfd->setfd)
  948. fd = receive_fd(addfd->file, addfd->flags);
  949. else
  950. fd = receive_fd_replace(addfd->fd, addfd->file, addfd->flags);
  951. addfd->ret = fd;
  952. if (addfd->ioctl_flags & SECCOMP_ADDFD_FLAG_SEND) {
  953. /* If we fail reset and return an error to the notifier */
  954. if (fd < 0) {
  955. n->state = SECCOMP_NOTIFY_SENT;
  956. } else {
  957. /* Return the FD we just added */
  958. n->flags = 0;
  959. n->error = 0;
  960. n->val = fd;
  961. }
  962. }
  963. /*
  964. * Mark the notification as completed. From this point, addfd mem
  965. * might be invalidated and we can't safely read it anymore.
  966. */
  967. complete(&addfd->completion);
  968. }
  969. static bool should_sleep_killable(struct seccomp_filter *match,
  970. struct seccomp_knotif *n)
  971. {
  972. return match->wait_killable_recv && n->state == SECCOMP_NOTIFY_SENT;
  973. }
  974. static int seccomp_do_user_notification(int this_syscall,
  975. struct seccomp_filter *match,
  976. const struct seccomp_data *sd)
  977. {
  978. int err;
  979. u32 flags = 0;
  980. long ret = 0;
  981. struct seccomp_knotif n = {};
  982. struct seccomp_kaddfd *addfd, *tmp;
  983. mutex_lock(&match->notify_lock);
  984. err = -ENOSYS;
  985. if (!match->notif)
  986. goto out;
  987. n.task = current;
  988. n.state = SECCOMP_NOTIFY_INIT;
  989. n.data = sd;
  990. n.id = seccomp_next_notify_id(match);
  991. init_completion(&n.ready);
  992. list_add_tail(&n.list, &match->notif->notifications);
  993. INIT_LIST_HEAD(&n.addfd);
  994. up(&match->notif->request);
  995. wake_up_poll(&match->wqh, EPOLLIN | EPOLLRDNORM);
  996. /*
  997. * This is where we wait for a reply from userspace.
  998. */
  999. do {
  1000. bool wait_killable = should_sleep_killable(match, &n);
  1001. mutex_unlock(&match->notify_lock);
  1002. if (wait_killable)
  1003. err = wait_for_completion_killable(&n.ready);
  1004. else
  1005. err = wait_for_completion_interruptible(&n.ready);
  1006. mutex_lock(&match->notify_lock);
  1007. if (err != 0) {
  1008. /*
  1009. * Check to see if the notifcation got picked up and
  1010. * whether we should switch to wait killable.
  1011. */
  1012. if (!wait_killable && should_sleep_killable(match, &n))
  1013. continue;
  1014. goto interrupted;
  1015. }
  1016. addfd = list_first_entry_or_null(&n.addfd,
  1017. struct seccomp_kaddfd, list);
  1018. /* Check if we were woken up by a addfd message */
  1019. if (addfd)
  1020. seccomp_handle_addfd(addfd, &n);
  1021. } while (n.state != SECCOMP_NOTIFY_REPLIED);
  1022. ret = n.val;
  1023. err = n.error;
  1024. flags = n.flags;
  1025. interrupted:
  1026. /* If there were any pending addfd calls, clear them out */
  1027. list_for_each_entry_safe(addfd, tmp, &n.addfd, list) {
  1028. /* The process went away before we got a chance to handle it */
  1029. addfd->ret = -ESRCH;
  1030. list_del_init(&addfd->list);
  1031. complete(&addfd->completion);
  1032. }
  1033. /*
  1034. * Note that it's possible the listener died in between the time when
  1035. * we were notified of a response (or a signal) and when we were able to
  1036. * re-acquire the lock, so only delete from the list if the
  1037. * notification actually exists.
  1038. *
  1039. * Also note that this test is only valid because there's no way to
  1040. * *reattach* to a notifier right now. If one is added, we'll need to
  1041. * keep track of the notif itself and make sure they match here.
  1042. */
  1043. if (match->notif)
  1044. list_del(&n.list);
  1045. out:
  1046. mutex_unlock(&match->notify_lock);
  1047. /* Userspace requests to continue the syscall. */
  1048. if (flags & SECCOMP_USER_NOTIF_FLAG_CONTINUE)
  1049. return 0;
  1050. syscall_set_return_value(current, current_pt_regs(),
  1051. err, ret);
  1052. return -1;
  1053. }
  1054. static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd,
  1055. const bool recheck_after_trace)
  1056. {
  1057. u32 filter_ret, action;
  1058. struct seccomp_filter *match = NULL;
  1059. int data;
  1060. struct seccomp_data sd_local;
  1061. /*
  1062. * Make sure that any changes to mode from another thread have
  1063. * been seen after SYSCALL_WORK_SECCOMP was seen.
  1064. */
  1065. smp_rmb();
  1066. if (!sd) {
  1067. populate_seccomp_data(&sd_local);
  1068. sd = &sd_local;
  1069. }
  1070. filter_ret = seccomp_run_filters(sd, &match);
  1071. data = filter_ret & SECCOMP_RET_DATA;
  1072. action = filter_ret & SECCOMP_RET_ACTION_FULL;
  1073. switch (action) {
  1074. case SECCOMP_RET_ERRNO:
  1075. /* Set low-order bits as an errno, capped at MAX_ERRNO. */
  1076. if (data > MAX_ERRNO)
  1077. data = MAX_ERRNO;
  1078. syscall_set_return_value(current, current_pt_regs(),
  1079. -data, 0);
  1080. goto skip;
  1081. case SECCOMP_RET_TRAP:
  1082. /* Show the handler the original registers. */
  1083. syscall_rollback(current, current_pt_regs());
  1084. /* Let the filter pass back 16 bits of data. */
  1085. force_sig_seccomp(this_syscall, data, false);
  1086. goto skip;
  1087. case SECCOMP_RET_TRACE:
  1088. /* We've been put in this state by the ptracer already. */
  1089. if (recheck_after_trace)
  1090. return 0;
  1091. /* ENOSYS these calls if there is no tracer attached. */
  1092. if (!ptrace_event_enabled(current, PTRACE_EVENT_SECCOMP)) {
  1093. syscall_set_return_value(current,
  1094. current_pt_regs(),
  1095. -ENOSYS, 0);
  1096. goto skip;
  1097. }
  1098. /* Allow the BPF to provide the event message */
  1099. ptrace_event(PTRACE_EVENT_SECCOMP, data);
  1100. /*
  1101. * The delivery of a fatal signal during event
  1102. * notification may silently skip tracer notification,
  1103. * which could leave us with a potentially unmodified
  1104. * syscall that the tracer would have liked to have
  1105. * changed. Since the process is about to die, we just
  1106. * force the syscall to be skipped and let the signal
  1107. * kill the process and correctly handle any tracer exit
  1108. * notifications.
  1109. */
  1110. if (fatal_signal_pending(current))
  1111. goto skip;
  1112. /* Check if the tracer forced the syscall to be skipped. */
  1113. this_syscall = syscall_get_nr(current, current_pt_regs());
  1114. if (this_syscall < 0)
  1115. goto skip;
  1116. /*
  1117. * Recheck the syscall, since it may have changed. This
  1118. * intentionally uses a NULL struct seccomp_data to force
  1119. * a reload of all registers. This does not goto skip since
  1120. * a skip would have already been reported.
  1121. */
  1122. if (__seccomp_filter(this_syscall, NULL, true))
  1123. return -1;
  1124. return 0;
  1125. case SECCOMP_RET_USER_NOTIF:
  1126. if (seccomp_do_user_notification(this_syscall, match, sd))
  1127. goto skip;
  1128. return 0;
  1129. case SECCOMP_RET_LOG:
  1130. seccomp_log(this_syscall, 0, action, true);
  1131. return 0;
  1132. case SECCOMP_RET_ALLOW:
  1133. /*
  1134. * Note that the "match" filter will always be NULL for
  1135. * this action since SECCOMP_RET_ALLOW is the starting
  1136. * state in seccomp_run_filters().
  1137. */
  1138. return 0;
  1139. case SECCOMP_RET_KILL_THREAD:
  1140. case SECCOMP_RET_KILL_PROCESS:
  1141. default:
  1142. current->seccomp.mode = SECCOMP_MODE_DEAD;
  1143. seccomp_log(this_syscall, SIGSYS, action, true);
  1144. /* Dump core only if this is the last remaining thread. */
  1145. if (action != SECCOMP_RET_KILL_THREAD ||
  1146. (atomic_read(&current->signal->live) == 1)) {
  1147. /* Show the original registers in the dump. */
  1148. syscall_rollback(current, current_pt_regs());
  1149. /* Trigger a coredump with SIGSYS */
  1150. force_sig_seccomp(this_syscall, data, true);
  1151. } else {
  1152. do_exit(SIGSYS);
  1153. }
  1154. return -1; /* skip the syscall go directly to signal handling */
  1155. }
  1156. unreachable();
  1157. skip:
  1158. seccomp_log(this_syscall, 0, action, match ? match->log : false);
  1159. return -1;
  1160. }
  1161. #else
  1162. static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd,
  1163. const bool recheck_after_trace)
  1164. {
  1165. BUG();
  1166. return -1;
  1167. }
  1168. #endif
  1169. int __secure_computing(const struct seccomp_data *sd)
  1170. {
  1171. int mode = current->seccomp.mode;
  1172. int this_syscall;
  1173. if (IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) &&
  1174. unlikely(current->ptrace & PT_SUSPEND_SECCOMP))
  1175. return 0;
  1176. this_syscall = sd ? sd->nr :
  1177. syscall_get_nr(current, current_pt_regs());
  1178. switch (mode) {
  1179. case SECCOMP_MODE_STRICT:
  1180. __secure_computing_strict(this_syscall); /* may call do_exit */
  1181. return 0;
  1182. case SECCOMP_MODE_FILTER:
  1183. return __seccomp_filter(this_syscall, sd, false);
  1184. /* Surviving SECCOMP_RET_KILL_* must be proactively impossible. */
  1185. case SECCOMP_MODE_DEAD:
  1186. WARN_ON_ONCE(1);
  1187. do_exit(SIGKILL);
  1188. return -1;
  1189. default:
  1190. BUG();
  1191. }
  1192. }
  1193. #endif /* CONFIG_HAVE_ARCH_SECCOMP_FILTER */
  1194. long prctl_get_seccomp(void)
  1195. {
  1196. return current->seccomp.mode;
  1197. }
  1198. /**
  1199. * seccomp_set_mode_strict: internal function for setting strict seccomp
  1200. *
  1201. * Once current->seccomp.mode is non-zero, it may not be changed.
  1202. *
  1203. * Returns 0 on success or -EINVAL on failure.
  1204. */
  1205. static long seccomp_set_mode_strict(void)
  1206. {
  1207. const unsigned long seccomp_mode = SECCOMP_MODE_STRICT;
  1208. long ret = -EINVAL;
  1209. spin_lock_irq(&current->sighand->siglock);
  1210. if (!seccomp_may_assign_mode(seccomp_mode))
  1211. goto out;
  1212. #ifdef TIF_NOTSC
  1213. disable_TSC();
  1214. #endif
  1215. seccomp_assign_mode(current, seccomp_mode, 0);
  1216. ret = 0;
  1217. out:
  1218. spin_unlock_irq(&current->sighand->siglock);
  1219. return ret;
  1220. }
  1221. #ifdef CONFIG_SECCOMP_FILTER
  1222. static void seccomp_notify_free(struct seccomp_filter *filter)
  1223. {
  1224. kfree(filter->notif);
  1225. filter->notif = NULL;
  1226. }
  1227. static void seccomp_notify_detach(struct seccomp_filter *filter)
  1228. {
  1229. struct seccomp_knotif *knotif;
  1230. if (!filter)
  1231. return;
  1232. mutex_lock(&filter->notify_lock);
  1233. /*
  1234. * If this file is being closed because e.g. the task who owned it
  1235. * died, let's wake everyone up who was waiting on us.
  1236. */
  1237. list_for_each_entry(knotif, &filter->notif->notifications, list) {
  1238. if (knotif->state == SECCOMP_NOTIFY_REPLIED)
  1239. continue;
  1240. knotif->state = SECCOMP_NOTIFY_REPLIED;
  1241. knotif->error = -ENOSYS;
  1242. knotif->val = 0;
  1243. /*
  1244. * We do not need to wake up any pending addfd messages, as
  1245. * the notifier will do that for us, as this just looks
  1246. * like a standard reply.
  1247. */
  1248. complete(&knotif->ready);
  1249. }
  1250. seccomp_notify_free(filter);
  1251. mutex_unlock(&filter->notify_lock);
  1252. }
  1253. static int seccomp_notify_release(struct inode *inode, struct file *file)
  1254. {
  1255. struct seccomp_filter *filter = file->private_data;
  1256. seccomp_notify_detach(filter);
  1257. __put_seccomp_filter(filter);
  1258. return 0;
  1259. }
  1260. /* must be called with notif_lock held */
  1261. static inline struct seccomp_knotif *
  1262. find_notification(struct seccomp_filter *filter, u64 id)
  1263. {
  1264. struct seccomp_knotif *cur;
  1265. lockdep_assert_held(&filter->notify_lock);
  1266. list_for_each_entry(cur, &filter->notif->notifications, list) {
  1267. if (cur->id == id)
  1268. return cur;
  1269. }
  1270. return NULL;
  1271. }
  1272. static long seccomp_notify_recv(struct seccomp_filter *filter,
  1273. void __user *buf)
  1274. {
  1275. struct seccomp_knotif *knotif = NULL, *cur;
  1276. struct seccomp_notif unotif;
  1277. ssize_t ret;
  1278. /* Verify that we're not given garbage to keep struct extensible. */
  1279. ret = check_zeroed_user(buf, sizeof(unotif));
  1280. if (ret < 0)
  1281. return ret;
  1282. if (!ret)
  1283. return -EINVAL;
  1284. memset(&unotif, 0, sizeof(unotif));
  1285. ret = down_interruptible(&filter->notif->request);
  1286. if (ret < 0)
  1287. return ret;
  1288. mutex_lock(&filter->notify_lock);
  1289. list_for_each_entry(cur, &filter->notif->notifications, list) {
  1290. if (cur->state == SECCOMP_NOTIFY_INIT) {
  1291. knotif = cur;
  1292. break;
  1293. }
  1294. }
  1295. /*
  1296. * If we didn't find a notification, it could be that the task was
  1297. * interrupted by a fatal signal between the time we were woken and
  1298. * when we were able to acquire the rw lock.
  1299. */
  1300. if (!knotif) {
  1301. ret = -ENOENT;
  1302. goto out;
  1303. }
  1304. unotif.id = knotif->id;
  1305. unotif.pid = task_pid_vnr(knotif->task);
  1306. unotif.data = *(knotif->data);
  1307. knotif->state = SECCOMP_NOTIFY_SENT;
  1308. wake_up_poll(&filter->wqh, EPOLLOUT | EPOLLWRNORM);
  1309. ret = 0;
  1310. out:
  1311. mutex_unlock(&filter->notify_lock);
  1312. if (ret == 0 && copy_to_user(buf, &unotif, sizeof(unotif))) {
  1313. ret = -EFAULT;
  1314. /*
  1315. * Userspace screwed up. To make sure that we keep this
  1316. * notification alive, let's reset it back to INIT. It
  1317. * may have died when we released the lock, so we need to make
  1318. * sure it's still around.
  1319. */
  1320. mutex_lock(&filter->notify_lock);
  1321. knotif = find_notification(filter, unotif.id);
  1322. if (knotif) {
  1323. /* Reset the process to make sure it's not stuck */
  1324. if (should_sleep_killable(filter, knotif))
  1325. complete(&knotif->ready);
  1326. knotif->state = SECCOMP_NOTIFY_INIT;
  1327. up(&filter->notif->request);
  1328. }
  1329. mutex_unlock(&filter->notify_lock);
  1330. }
  1331. return ret;
  1332. }
  1333. static long seccomp_notify_send(struct seccomp_filter *filter,
  1334. void __user *buf)
  1335. {
  1336. struct seccomp_notif_resp resp = {};
  1337. struct seccomp_knotif *knotif;
  1338. long ret;
  1339. if (copy_from_user(&resp, buf, sizeof(resp)))
  1340. return -EFAULT;
  1341. if (resp.flags & ~SECCOMP_USER_NOTIF_FLAG_CONTINUE)
  1342. return -EINVAL;
  1343. if ((resp.flags & SECCOMP_USER_NOTIF_FLAG_CONTINUE) &&
  1344. (resp.error || resp.val))
  1345. return -EINVAL;
  1346. ret = mutex_lock_interruptible(&filter->notify_lock);
  1347. if (ret < 0)
  1348. return ret;
  1349. knotif = find_notification(filter, resp.id);
  1350. if (!knotif) {
  1351. ret = -ENOENT;
  1352. goto out;
  1353. }
  1354. /* Allow exactly one reply. */
  1355. if (knotif->state != SECCOMP_NOTIFY_SENT) {
  1356. ret = -EINPROGRESS;
  1357. goto out;
  1358. }
  1359. ret = 0;
  1360. knotif->state = SECCOMP_NOTIFY_REPLIED;
  1361. knotif->error = resp.error;
  1362. knotif->val = resp.val;
  1363. knotif->flags = resp.flags;
  1364. complete(&knotif->ready);
  1365. out:
  1366. mutex_unlock(&filter->notify_lock);
  1367. return ret;
  1368. }
  1369. static long seccomp_notify_id_valid(struct seccomp_filter *filter,
  1370. void __user *buf)
  1371. {
  1372. struct seccomp_knotif *knotif;
  1373. u64 id;
  1374. long ret;
  1375. if (copy_from_user(&id, buf, sizeof(id)))
  1376. return -EFAULT;
  1377. ret = mutex_lock_interruptible(&filter->notify_lock);
  1378. if (ret < 0)
  1379. return ret;
  1380. knotif = find_notification(filter, id);
  1381. if (knotif && knotif->state == SECCOMP_NOTIFY_SENT)
  1382. ret = 0;
  1383. else
  1384. ret = -ENOENT;
  1385. mutex_unlock(&filter->notify_lock);
  1386. return ret;
  1387. }
  1388. static long seccomp_notify_addfd(struct seccomp_filter *filter,
  1389. struct seccomp_notif_addfd __user *uaddfd,
  1390. unsigned int size)
  1391. {
  1392. struct seccomp_notif_addfd addfd;
  1393. struct seccomp_knotif *knotif;
  1394. struct seccomp_kaddfd kaddfd;
  1395. int ret;
  1396. BUILD_BUG_ON(sizeof(addfd) < SECCOMP_NOTIFY_ADDFD_SIZE_VER0);
  1397. BUILD_BUG_ON(sizeof(addfd) != SECCOMP_NOTIFY_ADDFD_SIZE_LATEST);
  1398. if (size < SECCOMP_NOTIFY_ADDFD_SIZE_VER0 || size >= PAGE_SIZE)
  1399. return -EINVAL;
  1400. ret = copy_struct_from_user(&addfd, sizeof(addfd), uaddfd, size);
  1401. if (ret)
  1402. return ret;
  1403. if (addfd.newfd_flags & ~O_CLOEXEC)
  1404. return -EINVAL;
  1405. if (addfd.flags & ~(SECCOMP_ADDFD_FLAG_SETFD | SECCOMP_ADDFD_FLAG_SEND))
  1406. return -EINVAL;
  1407. if (addfd.newfd && !(addfd.flags & SECCOMP_ADDFD_FLAG_SETFD))
  1408. return -EINVAL;
  1409. kaddfd.file = fget(addfd.srcfd);
  1410. if (!kaddfd.file)
  1411. return -EBADF;
  1412. kaddfd.ioctl_flags = addfd.flags;
  1413. kaddfd.flags = addfd.newfd_flags;
  1414. kaddfd.setfd = addfd.flags & SECCOMP_ADDFD_FLAG_SETFD;
  1415. kaddfd.fd = addfd.newfd;
  1416. init_completion(&kaddfd.completion);
  1417. ret = mutex_lock_interruptible(&filter->notify_lock);
  1418. if (ret < 0)
  1419. goto out;
  1420. knotif = find_notification(filter, addfd.id);
  1421. if (!knotif) {
  1422. ret = -ENOENT;
  1423. goto out_unlock;
  1424. }
  1425. /*
  1426. * We do not want to allow for FD injection to occur before the
  1427. * notification has been picked up by a userspace handler, or after
  1428. * the notification has been replied to.
  1429. */
  1430. if (knotif->state != SECCOMP_NOTIFY_SENT) {
  1431. ret = -EINPROGRESS;
  1432. goto out_unlock;
  1433. }
  1434. if (addfd.flags & SECCOMP_ADDFD_FLAG_SEND) {
  1435. /*
  1436. * Disallow queuing an atomic addfd + send reply while there are
  1437. * some addfd requests still to process.
  1438. *
  1439. * There is no clear reason to support it and allows us to keep
  1440. * the loop on the other side straight-forward.
  1441. */
  1442. if (!list_empty(&knotif->addfd)) {
  1443. ret = -EBUSY;
  1444. goto out_unlock;
  1445. }
  1446. /* Allow exactly only one reply */
  1447. knotif->state = SECCOMP_NOTIFY_REPLIED;
  1448. }
  1449. list_add(&kaddfd.list, &knotif->addfd);
  1450. complete(&knotif->ready);
  1451. mutex_unlock(&filter->notify_lock);
  1452. /* Now we wait for it to be processed or be interrupted */
  1453. ret = wait_for_completion_interruptible(&kaddfd.completion);
  1454. if (ret == 0) {
  1455. /*
  1456. * We had a successful completion. The other side has already
  1457. * removed us from the addfd queue, and
  1458. * wait_for_completion_interruptible has a memory barrier upon
  1459. * success that lets us read this value directly without
  1460. * locking.
  1461. */
  1462. ret = kaddfd.ret;
  1463. goto out;
  1464. }
  1465. mutex_lock(&filter->notify_lock);
  1466. /*
  1467. * Even though we were woken up by a signal and not a successful
  1468. * completion, a completion may have happened in the mean time.
  1469. *
  1470. * We need to check again if the addfd request has been handled,
  1471. * and if not, we will remove it from the queue.
  1472. */
  1473. if (list_empty(&kaddfd.list))
  1474. ret = kaddfd.ret;
  1475. else
  1476. list_del(&kaddfd.list);
  1477. out_unlock:
  1478. mutex_unlock(&filter->notify_lock);
  1479. out:
  1480. fput(kaddfd.file);
  1481. return ret;
  1482. }
  1483. static long seccomp_notify_ioctl(struct file *file, unsigned int cmd,
  1484. unsigned long arg)
  1485. {
  1486. struct seccomp_filter *filter = file->private_data;
  1487. void __user *buf = (void __user *)arg;
  1488. /* Fixed-size ioctls */
  1489. switch (cmd) {
  1490. case SECCOMP_IOCTL_NOTIF_RECV:
  1491. return seccomp_notify_recv(filter, buf);
  1492. case SECCOMP_IOCTL_NOTIF_SEND:
  1493. return seccomp_notify_send(filter, buf);
  1494. case SECCOMP_IOCTL_NOTIF_ID_VALID_WRONG_DIR:
  1495. case SECCOMP_IOCTL_NOTIF_ID_VALID:
  1496. return seccomp_notify_id_valid(filter, buf);
  1497. }
  1498. /* Extensible Argument ioctls */
  1499. #define EA_IOCTL(cmd) ((cmd) & ~(IOC_INOUT | IOCSIZE_MASK))
  1500. switch (EA_IOCTL(cmd)) {
  1501. case EA_IOCTL(SECCOMP_IOCTL_NOTIF_ADDFD):
  1502. return seccomp_notify_addfd(filter, buf, _IOC_SIZE(cmd));
  1503. default:
  1504. return -EINVAL;
  1505. }
  1506. }
  1507. static __poll_t seccomp_notify_poll(struct file *file,
  1508. struct poll_table_struct *poll_tab)
  1509. {
  1510. struct seccomp_filter *filter = file->private_data;
  1511. __poll_t ret = 0;
  1512. struct seccomp_knotif *cur;
  1513. poll_wait(file, &filter->wqh, poll_tab);
  1514. if (mutex_lock_interruptible(&filter->notify_lock) < 0)
  1515. return EPOLLERR;
  1516. list_for_each_entry(cur, &filter->notif->notifications, list) {
  1517. if (cur->state == SECCOMP_NOTIFY_INIT)
  1518. ret |= EPOLLIN | EPOLLRDNORM;
  1519. if (cur->state == SECCOMP_NOTIFY_SENT)
  1520. ret |= EPOLLOUT | EPOLLWRNORM;
  1521. if ((ret & EPOLLIN) && (ret & EPOLLOUT))
  1522. break;
  1523. }
  1524. mutex_unlock(&filter->notify_lock);
  1525. if (refcount_read(&filter->users) == 0)
  1526. ret |= EPOLLHUP;
  1527. return ret;
  1528. }
  1529. static const struct file_operations seccomp_notify_ops = {
  1530. .poll = seccomp_notify_poll,
  1531. .release = seccomp_notify_release,
  1532. .unlocked_ioctl = seccomp_notify_ioctl,
  1533. .compat_ioctl = seccomp_notify_ioctl,
  1534. };
  1535. static struct file *init_listener(struct seccomp_filter *filter)
  1536. {
  1537. struct file *ret;
  1538. ret = ERR_PTR(-ENOMEM);
  1539. filter->notif = kzalloc(sizeof(*(filter->notif)), GFP_KERNEL);
  1540. if (!filter->notif)
  1541. goto out;
  1542. sema_init(&filter->notif->request, 0);
  1543. filter->notif->next_id = get_random_u64();
  1544. INIT_LIST_HEAD(&filter->notif->notifications);
  1545. ret = anon_inode_getfile("seccomp notify", &seccomp_notify_ops,
  1546. filter, O_RDWR);
  1547. if (IS_ERR(ret))
  1548. goto out_notif;
  1549. /* The file has a reference to it now */
  1550. __get_seccomp_filter(filter);
  1551. out_notif:
  1552. if (IS_ERR(ret))
  1553. seccomp_notify_free(filter);
  1554. out:
  1555. return ret;
  1556. }
  1557. /*
  1558. * Does @new_child have a listener while an ancestor also has a listener?
  1559. * If so, we'll want to reject this filter.
  1560. * This only has to be tested for the current process, even in the TSYNC case,
  1561. * because TSYNC installs @child with the same parent on all threads.
  1562. * Note that @new_child is not hooked up to its parent at this point yet, so
  1563. * we use current->seccomp.filter.
  1564. */
  1565. static bool has_duplicate_listener(struct seccomp_filter *new_child)
  1566. {
  1567. struct seccomp_filter *cur;
  1568. /* must be protected against concurrent TSYNC */
  1569. lockdep_assert_held(&current->sighand->siglock);
  1570. if (!new_child->notif)
  1571. return false;
  1572. for (cur = current->seccomp.filter; cur; cur = cur->prev) {
  1573. if (cur->notif)
  1574. return true;
  1575. }
  1576. return false;
  1577. }
  1578. /**
  1579. * seccomp_set_mode_filter: internal function for setting seccomp filter
  1580. * @flags: flags to change filter behavior
  1581. * @filter: struct sock_fprog containing filter
  1582. *
  1583. * This function may be called repeatedly to install additional filters.
  1584. * Every filter successfully installed will be evaluated (in reverse order)
  1585. * for each system call the task makes.
  1586. *
  1587. * Once current->seccomp.mode is non-zero, it may not be changed.
  1588. *
  1589. * Returns 0 on success or -EINVAL on failure.
  1590. */
  1591. static long seccomp_set_mode_filter(unsigned int flags,
  1592. const char __user *filter)
  1593. {
  1594. const unsigned long seccomp_mode = SECCOMP_MODE_FILTER;
  1595. struct seccomp_filter *prepared = NULL;
  1596. long ret = -EINVAL;
  1597. int listener = -1;
  1598. struct file *listener_f = NULL;
  1599. /* Validate flags. */
  1600. if (flags & ~SECCOMP_FILTER_FLAG_MASK)
  1601. return -EINVAL;
  1602. /*
  1603. * In the successful case, NEW_LISTENER returns the new listener fd.
  1604. * But in the failure case, TSYNC returns the thread that died. If you
  1605. * combine these two flags, there's no way to tell whether something
  1606. * succeeded or failed. So, let's disallow this combination if the user
  1607. * has not explicitly requested no errors from TSYNC.
  1608. */
  1609. if ((flags & SECCOMP_FILTER_FLAG_TSYNC) &&
  1610. (flags & SECCOMP_FILTER_FLAG_NEW_LISTENER) &&
  1611. ((flags & SECCOMP_FILTER_FLAG_TSYNC_ESRCH) == 0))
  1612. return -EINVAL;
  1613. /*
  1614. * The SECCOMP_FILTER_FLAG_WAIT_KILLABLE_SENT flag doesn't make sense
  1615. * without the SECCOMP_FILTER_FLAG_NEW_LISTENER flag.
  1616. */
  1617. if ((flags & SECCOMP_FILTER_FLAG_WAIT_KILLABLE_RECV) &&
  1618. ((flags & SECCOMP_FILTER_FLAG_NEW_LISTENER) == 0))
  1619. return -EINVAL;
  1620. /* Prepare the new filter before holding any locks. */
  1621. prepared = seccomp_prepare_user_filter(filter);
  1622. if (IS_ERR(prepared))
  1623. return PTR_ERR(prepared);
  1624. if (flags & SECCOMP_FILTER_FLAG_NEW_LISTENER) {
  1625. listener = get_unused_fd_flags(O_CLOEXEC);
  1626. if (listener < 0) {
  1627. ret = listener;
  1628. goto out_free;
  1629. }
  1630. listener_f = init_listener(prepared);
  1631. if (IS_ERR(listener_f)) {
  1632. put_unused_fd(listener);
  1633. ret = PTR_ERR(listener_f);
  1634. goto out_free;
  1635. }
  1636. }
  1637. /*
  1638. * Make sure we cannot change seccomp or nnp state via TSYNC
  1639. * while another thread is in the middle of calling exec.
  1640. */
  1641. if (flags & SECCOMP_FILTER_FLAG_TSYNC &&
  1642. mutex_lock_killable(&current->signal->cred_guard_mutex))
  1643. goto out_put_fd;
  1644. spin_lock_irq(&current->sighand->siglock);
  1645. if (!seccomp_may_assign_mode(seccomp_mode))
  1646. goto out;
  1647. if (has_duplicate_listener(prepared)) {
  1648. ret = -EBUSY;
  1649. goto out;
  1650. }
  1651. ret = seccomp_attach_filter(flags, prepared);
  1652. if (ret)
  1653. goto out;
  1654. /* Do not free the successfully attached filter. */
  1655. prepared = NULL;
  1656. seccomp_assign_mode(current, seccomp_mode, flags);
  1657. out:
  1658. spin_unlock_irq(&current->sighand->siglock);
  1659. if (flags & SECCOMP_FILTER_FLAG_TSYNC)
  1660. mutex_unlock(&current->signal->cred_guard_mutex);
  1661. out_put_fd:
  1662. if (flags & SECCOMP_FILTER_FLAG_NEW_LISTENER) {
  1663. if (ret) {
  1664. listener_f->private_data = NULL;
  1665. fput(listener_f);
  1666. put_unused_fd(listener);
  1667. seccomp_notify_detach(prepared);
  1668. } else {
  1669. fd_install(listener, listener_f);
  1670. ret = listener;
  1671. }
  1672. }
  1673. out_free:
  1674. seccomp_filter_free(prepared);
  1675. return ret;
  1676. }
  1677. #else
  1678. static inline long seccomp_set_mode_filter(unsigned int flags,
  1679. const char __user *filter)
  1680. {
  1681. return -EINVAL;
  1682. }
  1683. #endif
  1684. static long seccomp_get_action_avail(const char __user *uaction)
  1685. {
  1686. u32 action;
  1687. if (copy_from_user(&action, uaction, sizeof(action)))
  1688. return -EFAULT;
  1689. switch (action) {
  1690. case SECCOMP_RET_KILL_PROCESS:
  1691. case SECCOMP_RET_KILL_THREAD:
  1692. case SECCOMP_RET_TRAP:
  1693. case SECCOMP_RET_ERRNO:
  1694. case SECCOMP_RET_USER_NOTIF:
  1695. case SECCOMP_RET_TRACE:
  1696. case SECCOMP_RET_LOG:
  1697. case SECCOMP_RET_ALLOW:
  1698. break;
  1699. default:
  1700. return -EOPNOTSUPP;
  1701. }
  1702. return 0;
  1703. }
  1704. static long seccomp_get_notif_sizes(void __user *usizes)
  1705. {
  1706. struct seccomp_notif_sizes sizes = {
  1707. .seccomp_notif = sizeof(struct seccomp_notif),
  1708. .seccomp_notif_resp = sizeof(struct seccomp_notif_resp),
  1709. .seccomp_data = sizeof(struct seccomp_data),
  1710. };
  1711. if (copy_to_user(usizes, &sizes, sizeof(sizes)))
  1712. return -EFAULT;
  1713. return 0;
  1714. }
  1715. /* Common entry point for both prctl and syscall. */
  1716. static long do_seccomp(unsigned int op, unsigned int flags,
  1717. void __user *uargs)
  1718. {
  1719. switch (op) {
  1720. case SECCOMP_SET_MODE_STRICT:
  1721. if (flags != 0 || uargs != NULL)
  1722. return -EINVAL;
  1723. return seccomp_set_mode_strict();
  1724. case SECCOMP_SET_MODE_FILTER:
  1725. return seccomp_set_mode_filter(flags, uargs);
  1726. case SECCOMP_GET_ACTION_AVAIL:
  1727. if (flags != 0)
  1728. return -EINVAL;
  1729. return seccomp_get_action_avail(uargs);
  1730. case SECCOMP_GET_NOTIF_SIZES:
  1731. if (flags != 0)
  1732. return -EINVAL;
  1733. return seccomp_get_notif_sizes(uargs);
  1734. default:
  1735. return -EINVAL;
  1736. }
  1737. }
  1738. SYSCALL_DEFINE3(seccomp, unsigned int, op, unsigned int, flags,
  1739. void __user *, uargs)
  1740. {
  1741. return do_seccomp(op, flags, uargs);
  1742. }
  1743. /**
  1744. * prctl_set_seccomp: configures current->seccomp.mode
  1745. * @seccomp_mode: requested mode to use
  1746. * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
  1747. *
  1748. * Returns 0 on success or -EINVAL on failure.
  1749. */
  1750. long prctl_set_seccomp(unsigned long seccomp_mode, void __user *filter)
  1751. {
  1752. unsigned int op;
  1753. void __user *uargs;
  1754. switch (seccomp_mode) {
  1755. case SECCOMP_MODE_STRICT:
  1756. op = SECCOMP_SET_MODE_STRICT;
  1757. /*
  1758. * Setting strict mode through prctl always ignored filter,
  1759. * so make sure it is always NULL here to pass the internal
  1760. * check in do_seccomp().
  1761. */
  1762. uargs = NULL;
  1763. break;
  1764. case SECCOMP_MODE_FILTER:
  1765. op = SECCOMP_SET_MODE_FILTER;
  1766. uargs = filter;
  1767. break;
  1768. default:
  1769. return -EINVAL;
  1770. }
  1771. /* prctl interface doesn't have flags, so they are always zero. */
  1772. return do_seccomp(op, 0, uargs);
  1773. }
  1774. #if defined(CONFIG_SECCOMP_FILTER) && defined(CONFIG_CHECKPOINT_RESTORE)
  1775. static struct seccomp_filter *get_nth_filter(struct task_struct *task,
  1776. unsigned long filter_off)
  1777. {
  1778. struct seccomp_filter *orig, *filter;
  1779. unsigned long count;
  1780. /*
  1781. * Note: this is only correct because the caller should be the (ptrace)
  1782. * tracer of the task, otherwise lock_task_sighand is needed.
  1783. */
  1784. spin_lock_irq(&task->sighand->siglock);
  1785. if (task->seccomp.mode != SECCOMP_MODE_FILTER) {
  1786. spin_unlock_irq(&task->sighand->siglock);
  1787. return ERR_PTR(-EINVAL);
  1788. }
  1789. orig = task->seccomp.filter;
  1790. __get_seccomp_filter(orig);
  1791. spin_unlock_irq(&task->sighand->siglock);
  1792. count = 0;
  1793. for (filter = orig; filter; filter = filter->prev)
  1794. count++;
  1795. if (filter_off >= count) {
  1796. filter = ERR_PTR(-ENOENT);
  1797. goto out;
  1798. }
  1799. count -= filter_off;
  1800. for (filter = orig; filter && count > 1; filter = filter->prev)
  1801. count--;
  1802. if (WARN_ON(count != 1 || !filter)) {
  1803. filter = ERR_PTR(-ENOENT);
  1804. goto out;
  1805. }
  1806. __get_seccomp_filter(filter);
  1807. out:
  1808. __put_seccomp_filter(orig);
  1809. return filter;
  1810. }
  1811. long seccomp_get_filter(struct task_struct *task, unsigned long filter_off,
  1812. void __user *data)
  1813. {
  1814. struct seccomp_filter *filter;
  1815. struct sock_fprog_kern *fprog;
  1816. long ret;
  1817. if (!capable(CAP_SYS_ADMIN) ||
  1818. current->seccomp.mode != SECCOMP_MODE_DISABLED) {
  1819. return -EACCES;
  1820. }
  1821. filter = get_nth_filter(task, filter_off);
  1822. if (IS_ERR(filter))
  1823. return PTR_ERR(filter);
  1824. fprog = filter->prog->orig_prog;
  1825. if (!fprog) {
  1826. /* This must be a new non-cBPF filter, since we save
  1827. * every cBPF filter's orig_prog above when
  1828. * CONFIG_CHECKPOINT_RESTORE is enabled.
  1829. */
  1830. ret = -EMEDIUMTYPE;
  1831. goto out;
  1832. }
  1833. ret = fprog->len;
  1834. if (!data)
  1835. goto out;
  1836. if (copy_to_user(data, fprog->filter, bpf_classic_proglen(fprog)))
  1837. ret = -EFAULT;
  1838. out:
  1839. __put_seccomp_filter(filter);
  1840. return ret;
  1841. }
  1842. long seccomp_get_metadata(struct task_struct *task,
  1843. unsigned long size, void __user *data)
  1844. {
  1845. long ret;
  1846. struct seccomp_filter *filter;
  1847. struct seccomp_metadata kmd = {};
  1848. if (!capable(CAP_SYS_ADMIN) ||
  1849. current->seccomp.mode != SECCOMP_MODE_DISABLED) {
  1850. return -EACCES;
  1851. }
  1852. size = min_t(unsigned long, size, sizeof(kmd));
  1853. if (size < sizeof(kmd.filter_off))
  1854. return -EINVAL;
  1855. if (copy_from_user(&kmd.filter_off, data, sizeof(kmd.filter_off)))
  1856. return -EFAULT;
  1857. filter = get_nth_filter(task, kmd.filter_off);
  1858. if (IS_ERR(filter))
  1859. return PTR_ERR(filter);
  1860. if (filter->log)
  1861. kmd.flags |= SECCOMP_FILTER_FLAG_LOG;
  1862. ret = size;
  1863. if (copy_to_user(data, &kmd, size))
  1864. ret = -EFAULT;
  1865. __put_seccomp_filter(filter);
  1866. return ret;
  1867. }
  1868. #endif
  1869. #ifdef CONFIG_SYSCTL
  1870. /* Human readable action names for friendly sysctl interaction */
  1871. #define SECCOMP_RET_KILL_PROCESS_NAME "kill_process"
  1872. #define SECCOMP_RET_KILL_THREAD_NAME "kill_thread"
  1873. #define SECCOMP_RET_TRAP_NAME "trap"
  1874. #define SECCOMP_RET_ERRNO_NAME "errno"
  1875. #define SECCOMP_RET_USER_NOTIF_NAME "user_notif"
  1876. #define SECCOMP_RET_TRACE_NAME "trace"
  1877. #define SECCOMP_RET_LOG_NAME "log"
  1878. #define SECCOMP_RET_ALLOW_NAME "allow"
  1879. static const char seccomp_actions_avail[] =
  1880. SECCOMP_RET_KILL_PROCESS_NAME " "
  1881. SECCOMP_RET_KILL_THREAD_NAME " "
  1882. SECCOMP_RET_TRAP_NAME " "
  1883. SECCOMP_RET_ERRNO_NAME " "
  1884. SECCOMP_RET_USER_NOTIF_NAME " "
  1885. SECCOMP_RET_TRACE_NAME " "
  1886. SECCOMP_RET_LOG_NAME " "
  1887. SECCOMP_RET_ALLOW_NAME;
  1888. struct seccomp_log_name {
  1889. u32 log;
  1890. const char *name;
  1891. };
  1892. static const struct seccomp_log_name seccomp_log_names[] = {
  1893. { SECCOMP_LOG_KILL_PROCESS, SECCOMP_RET_KILL_PROCESS_NAME },
  1894. { SECCOMP_LOG_KILL_THREAD, SECCOMP_RET_KILL_THREAD_NAME },
  1895. { SECCOMP_LOG_TRAP, SECCOMP_RET_TRAP_NAME },
  1896. { SECCOMP_LOG_ERRNO, SECCOMP_RET_ERRNO_NAME },
  1897. { SECCOMP_LOG_USER_NOTIF, SECCOMP_RET_USER_NOTIF_NAME },
  1898. { SECCOMP_LOG_TRACE, SECCOMP_RET_TRACE_NAME },
  1899. { SECCOMP_LOG_LOG, SECCOMP_RET_LOG_NAME },
  1900. { SECCOMP_LOG_ALLOW, SECCOMP_RET_ALLOW_NAME },
  1901. { }
  1902. };
  1903. static bool seccomp_names_from_actions_logged(char *names, size_t size,
  1904. u32 actions_logged,
  1905. const char *sep)
  1906. {
  1907. const struct seccomp_log_name *cur;
  1908. bool append_sep = false;
  1909. for (cur = seccomp_log_names; cur->name && size; cur++) {
  1910. ssize_t ret;
  1911. if (!(actions_logged & cur->log))
  1912. continue;
  1913. if (append_sep) {
  1914. ret = strscpy(names, sep, size);
  1915. if (ret < 0)
  1916. return false;
  1917. names += ret;
  1918. size -= ret;
  1919. } else
  1920. append_sep = true;
  1921. ret = strscpy(names, cur->name, size);
  1922. if (ret < 0)
  1923. return false;
  1924. names += ret;
  1925. size -= ret;
  1926. }
  1927. return true;
  1928. }
  1929. static bool seccomp_action_logged_from_name(u32 *action_logged,
  1930. const char *name)
  1931. {
  1932. const struct seccomp_log_name *cur;
  1933. for (cur = seccomp_log_names; cur->name; cur++) {
  1934. if (!strcmp(cur->name, name)) {
  1935. *action_logged = cur->log;
  1936. return true;
  1937. }
  1938. }
  1939. return false;
  1940. }
  1941. static bool seccomp_actions_logged_from_names(u32 *actions_logged, char *names)
  1942. {
  1943. char *name;
  1944. *actions_logged = 0;
  1945. while ((name = strsep(&names, " ")) && *name) {
  1946. u32 action_logged = 0;
  1947. if (!seccomp_action_logged_from_name(&action_logged, name))
  1948. return false;
  1949. *actions_logged |= action_logged;
  1950. }
  1951. return true;
  1952. }
  1953. static int read_actions_logged(struct ctl_table *ro_table, void *buffer,
  1954. size_t *lenp, loff_t *ppos)
  1955. {
  1956. char names[sizeof(seccomp_actions_avail)];
  1957. struct ctl_table table;
  1958. memset(names, 0, sizeof(names));
  1959. if (!seccomp_names_from_actions_logged(names, sizeof(names),
  1960. seccomp_actions_logged, " "))
  1961. return -EINVAL;
  1962. table = *ro_table;
  1963. table.data = names;
  1964. table.maxlen = sizeof(names);
  1965. return proc_dostring(&table, 0, buffer, lenp, ppos);
  1966. }
  1967. static int write_actions_logged(struct ctl_table *ro_table, void *buffer,
  1968. size_t *lenp, loff_t *ppos, u32 *actions_logged)
  1969. {
  1970. char names[sizeof(seccomp_actions_avail)];
  1971. struct ctl_table table;
  1972. int ret;
  1973. if (!capable(CAP_SYS_ADMIN))
  1974. return -EPERM;
  1975. memset(names, 0, sizeof(names));
  1976. table = *ro_table;
  1977. table.data = names;
  1978. table.maxlen = sizeof(names);
  1979. ret = proc_dostring(&table, 1, buffer, lenp, ppos);
  1980. if (ret)
  1981. return ret;
  1982. if (!seccomp_actions_logged_from_names(actions_logged, table.data))
  1983. return -EINVAL;
  1984. if (*actions_logged & SECCOMP_LOG_ALLOW)
  1985. return -EINVAL;
  1986. seccomp_actions_logged = *actions_logged;
  1987. return 0;
  1988. }
  1989. static void audit_actions_logged(u32 actions_logged, u32 old_actions_logged,
  1990. int ret)
  1991. {
  1992. char names[sizeof(seccomp_actions_avail)];
  1993. char old_names[sizeof(seccomp_actions_avail)];
  1994. const char *new = names;
  1995. const char *old = old_names;
  1996. if (!audit_enabled)
  1997. return;
  1998. memset(names, 0, sizeof(names));
  1999. memset(old_names, 0, sizeof(old_names));
  2000. if (ret)
  2001. new = "?";
  2002. else if (!actions_logged)
  2003. new = "(none)";
  2004. else if (!seccomp_names_from_actions_logged(names, sizeof(names),
  2005. actions_logged, ","))
  2006. new = "?";
  2007. if (!old_actions_logged)
  2008. old = "(none)";
  2009. else if (!seccomp_names_from_actions_logged(old_names,
  2010. sizeof(old_names),
  2011. old_actions_logged, ","))
  2012. old = "?";
  2013. return audit_seccomp_actions_logged(new, old, !ret);
  2014. }
  2015. static int seccomp_actions_logged_handler(struct ctl_table *ro_table, int write,
  2016. void *buffer, size_t *lenp,
  2017. loff_t *ppos)
  2018. {
  2019. int ret;
  2020. if (write) {
  2021. u32 actions_logged = 0;
  2022. u32 old_actions_logged = seccomp_actions_logged;
  2023. ret = write_actions_logged(ro_table, buffer, lenp, ppos,
  2024. &actions_logged);
  2025. audit_actions_logged(actions_logged, old_actions_logged, ret);
  2026. } else
  2027. ret = read_actions_logged(ro_table, buffer, lenp, ppos);
  2028. return ret;
  2029. }
  2030. static struct ctl_path seccomp_sysctl_path[] = {
  2031. { .procname = "kernel", },
  2032. { .procname = "seccomp", },
  2033. { }
  2034. };
  2035. static struct ctl_table seccomp_sysctl_table[] = {
  2036. {
  2037. .procname = "actions_avail",
  2038. .data = (void *) &seccomp_actions_avail,
  2039. .maxlen = sizeof(seccomp_actions_avail),
  2040. .mode = 0444,
  2041. .proc_handler = proc_dostring,
  2042. },
  2043. {
  2044. .procname = "actions_logged",
  2045. .mode = 0644,
  2046. .proc_handler = seccomp_actions_logged_handler,
  2047. },
  2048. { }
  2049. };
  2050. static int __init seccomp_sysctl_init(void)
  2051. {
  2052. struct ctl_table_header *hdr;
  2053. hdr = register_sysctl_paths(seccomp_sysctl_path, seccomp_sysctl_table);
  2054. if (!hdr)
  2055. pr_warn("sysctl registration failed\n");
  2056. else
  2057. kmemleak_not_leak(hdr);
  2058. return 0;
  2059. }
  2060. device_initcall(seccomp_sysctl_init)
  2061. #endif /* CONFIG_SYSCTL */
  2062. #ifdef CONFIG_SECCOMP_CACHE_DEBUG
  2063. /* Currently CONFIG_SECCOMP_CACHE_DEBUG implies SECCOMP_ARCH_NATIVE */
  2064. static void proc_pid_seccomp_cache_arch(struct seq_file *m, const char *name,
  2065. const void *bitmap, size_t bitmap_size)
  2066. {
  2067. int nr;
  2068. for (nr = 0; nr < bitmap_size; nr++) {
  2069. bool cached = test_bit(nr, bitmap);
  2070. char *status = cached ? "ALLOW" : "FILTER";
  2071. seq_printf(m, "%s %d %s\n", name, nr, status);
  2072. }
  2073. }
  2074. int proc_pid_seccomp_cache(struct seq_file *m, struct pid_namespace *ns,
  2075. struct pid *pid, struct task_struct *task)
  2076. {
  2077. struct seccomp_filter *f;
  2078. unsigned long flags;
  2079. /*
  2080. * We don't want some sandboxed process to know what their seccomp
  2081. * filters consist of.
  2082. */
  2083. if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN))
  2084. return -EACCES;
  2085. if (!lock_task_sighand(task, &flags))
  2086. return -ESRCH;
  2087. f = READ_ONCE(task->seccomp.filter);
  2088. if (!f) {
  2089. unlock_task_sighand(task, &flags);
  2090. return 0;
  2091. }
  2092. /* prevent filter from being freed while we are printing it */
  2093. __get_seccomp_filter(f);
  2094. unlock_task_sighand(task, &flags);
  2095. proc_pid_seccomp_cache_arch(m, SECCOMP_ARCH_NATIVE_NAME,
  2096. f->cache.allow_native,
  2097. SECCOMP_ARCH_NATIVE_NR);
  2098. #ifdef SECCOMP_ARCH_COMPAT
  2099. proc_pid_seccomp_cache_arch(m, SECCOMP_ARCH_COMPAT_NAME,
  2100. f->cache.allow_compat,
  2101. SECCOMP_ARCH_COMPAT_NR);
  2102. #endif /* SECCOMP_ARCH_COMPAT */
  2103. __put_seccomp_filter(f);
  2104. return 0;
  2105. }
  2106. #endif /* CONFIG_SECCOMP_CACHE_DEBUG */