seccomp.c 65 KB

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