seccomp.c 60 KB

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