auditsc.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452
  1. /* auditsc.c -- System-call auditing support
  2. * Handles all system-call specific auditing features.
  3. *
  4. * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
  5. * Copyright 2005 Hewlett-Packard Development Company, L.P.
  6. * Copyright (C) 2005, 2006 IBM Corporation
  7. * All Rights Reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. * Written by Rickard E. (Rik) Faith <faith@redhat.com>
  24. *
  25. * Many of the ideas implemented here are from Stephen C. Tweedie,
  26. * especially the idea of avoiding a copy by using getname.
  27. *
  28. * The method for actual interception of syscall entry and exit (not in
  29. * this file -- see entry.S) is based on a GPL'd patch written by
  30. * okir@suse.de and Copyright 2003 SuSE Linux AG.
  31. *
  32. * POSIX message queue support added by George Wilson <ltcgcw@us.ibm.com>,
  33. * 2006.
  34. *
  35. * The support of additional filter rules compares (>, <, >=, <=) was
  36. * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
  37. *
  38. * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
  39. * filesystem information.
  40. *
  41. * Subject and object context labeling support added by <danjones@us.ibm.com>
  42. * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance.
  43. */
  44. #include <linux/init.h>
  45. #include <asm/types.h>
  46. #include <linux/atomic.h>
  47. #include <linux/fs.h>
  48. #include <linux/namei.h>
  49. #include <linux/mm.h>
  50. #include <linux/export.h>
  51. #include <linux/slab.h>
  52. #include <linux/mount.h>
  53. #include <linux/socket.h>
  54. #include <linux/mqueue.h>
  55. #include <linux/audit.h>
  56. #include <linux/personality.h>
  57. #include <linux/time.h>
  58. #include <linux/netlink.h>
  59. #include <linux/compiler.h>
  60. #include <asm/unistd.h>
  61. #include <linux/security.h>
  62. #include <linux/list.h>
  63. #include <linux/tty.h>
  64. #include <linux/binfmts.h>
  65. #include <linux/highmem.h>
  66. #include <linux/syscalls.h>
  67. #include <linux/capability.h>
  68. #include <linux/fs_struct.h>
  69. #include <linux/compat.h>
  70. #include "audit.h"
  71. /* flags stating the success for a syscall */
  72. #define AUDITSC_INVALID 0
  73. #define AUDITSC_SUCCESS 1
  74. #define AUDITSC_FAILURE 2
  75. /* no execve audit message should be longer than this (userspace limits) */
  76. #define MAX_EXECVE_AUDIT_LEN 7500
  77. /* number of audit rules */
  78. int audit_n_rules;
  79. /* determines whether we collect data for signals sent */
  80. int audit_signals;
  81. struct audit_aux_data {
  82. struct audit_aux_data *next;
  83. int type;
  84. };
  85. #define AUDIT_AUX_IPCPERM 0
  86. /* Number of target pids per aux struct. */
  87. #define AUDIT_AUX_PIDS 16
  88. struct audit_aux_data_execve {
  89. struct audit_aux_data d;
  90. int argc;
  91. int envc;
  92. struct mm_struct *mm;
  93. };
  94. struct audit_aux_data_pids {
  95. struct audit_aux_data d;
  96. pid_t target_pid[AUDIT_AUX_PIDS];
  97. kuid_t target_auid[AUDIT_AUX_PIDS];
  98. kuid_t target_uid[AUDIT_AUX_PIDS];
  99. unsigned int target_sessionid[AUDIT_AUX_PIDS];
  100. u32 target_sid[AUDIT_AUX_PIDS];
  101. char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
  102. int pid_count;
  103. };
  104. struct audit_aux_data_bprm_fcaps {
  105. struct audit_aux_data d;
  106. struct audit_cap_data fcap;
  107. unsigned int fcap_ver;
  108. struct audit_cap_data old_pcap;
  109. struct audit_cap_data new_pcap;
  110. };
  111. struct audit_aux_data_capset {
  112. struct audit_aux_data d;
  113. pid_t pid;
  114. struct audit_cap_data cap;
  115. };
  116. struct audit_tree_refs {
  117. struct audit_tree_refs *next;
  118. struct audit_chunk *c[31];
  119. };
  120. static inline int open_arg(int flags, int mask)
  121. {
  122. int n = ACC_MODE(flags);
  123. if (flags & (O_TRUNC | O_CREAT))
  124. n |= AUDIT_PERM_WRITE;
  125. return n & mask;
  126. }
  127. static int audit_match_perm(struct audit_context *ctx, int mask)
  128. {
  129. unsigned n;
  130. if (unlikely(!ctx))
  131. return 0;
  132. n = ctx->major;
  133. switch (audit_classify_syscall(ctx->arch, n)) {
  134. case 0: /* native */
  135. if ((mask & AUDIT_PERM_WRITE) &&
  136. audit_match_class(AUDIT_CLASS_WRITE, n))
  137. return 1;
  138. if ((mask & AUDIT_PERM_READ) &&
  139. audit_match_class(AUDIT_CLASS_READ, n))
  140. return 1;
  141. if ((mask & AUDIT_PERM_ATTR) &&
  142. audit_match_class(AUDIT_CLASS_CHATTR, n))
  143. return 1;
  144. return 0;
  145. case 1: /* 32bit on biarch */
  146. if ((mask & AUDIT_PERM_WRITE) &&
  147. audit_match_class(AUDIT_CLASS_WRITE_32, n))
  148. return 1;
  149. if ((mask & AUDIT_PERM_READ) &&
  150. audit_match_class(AUDIT_CLASS_READ_32, n))
  151. return 1;
  152. if ((mask & AUDIT_PERM_ATTR) &&
  153. audit_match_class(AUDIT_CLASS_CHATTR_32, n))
  154. return 1;
  155. return 0;
  156. case 2: /* open */
  157. return mask & ACC_MODE(ctx->argv[1]);
  158. case 3: /* openat */
  159. return mask & ACC_MODE(ctx->argv[2]);
  160. case 4: /* socketcall */
  161. return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
  162. case 5: /* execve */
  163. return mask & AUDIT_PERM_EXEC;
  164. default:
  165. return 0;
  166. }
  167. }
  168. static int audit_match_filetype(struct audit_context *ctx, int val)
  169. {
  170. struct audit_names *n;
  171. umode_t mode = (umode_t)val;
  172. if (unlikely(!ctx))
  173. return 0;
  174. list_for_each_entry(n, &ctx->names_list, list) {
  175. if ((n->ino != -1) &&
  176. ((n->mode & S_IFMT) == mode))
  177. return 1;
  178. }
  179. return 0;
  180. }
  181. /*
  182. * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
  183. * ->first_trees points to its beginning, ->trees - to the current end of data.
  184. * ->tree_count is the number of free entries in array pointed to by ->trees.
  185. * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
  186. * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
  187. * it's going to remain 1-element for almost any setup) until we free context itself.
  188. * References in it _are_ dropped - at the same time we free/drop aux stuff.
  189. */
  190. #ifdef CONFIG_AUDIT_TREE
  191. static void audit_set_auditable(struct audit_context *ctx)
  192. {
  193. if (!ctx->prio) {
  194. ctx->prio = 1;
  195. ctx->current_state = AUDIT_RECORD_CONTEXT;
  196. }
  197. }
  198. static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
  199. {
  200. struct audit_tree_refs *p = ctx->trees;
  201. int left = ctx->tree_count;
  202. if (likely(left)) {
  203. p->c[--left] = chunk;
  204. ctx->tree_count = left;
  205. return 1;
  206. }
  207. if (!p)
  208. return 0;
  209. p = p->next;
  210. if (p) {
  211. p->c[30] = chunk;
  212. ctx->trees = p;
  213. ctx->tree_count = 30;
  214. return 1;
  215. }
  216. return 0;
  217. }
  218. static int grow_tree_refs(struct audit_context *ctx)
  219. {
  220. struct audit_tree_refs *p = ctx->trees;
  221. ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
  222. if (!ctx->trees) {
  223. ctx->trees = p;
  224. return 0;
  225. }
  226. if (p)
  227. p->next = ctx->trees;
  228. else
  229. ctx->first_trees = ctx->trees;
  230. ctx->tree_count = 31;
  231. return 1;
  232. }
  233. #endif
  234. static void unroll_tree_refs(struct audit_context *ctx,
  235. struct audit_tree_refs *p, int count)
  236. {
  237. #ifdef CONFIG_AUDIT_TREE
  238. struct audit_tree_refs *q;
  239. int n;
  240. if (!p) {
  241. /* we started with empty chain */
  242. p = ctx->first_trees;
  243. count = 31;
  244. /* if the very first allocation has failed, nothing to do */
  245. if (!p)
  246. return;
  247. }
  248. n = count;
  249. for (q = p; q != ctx->trees; q = q->next, n = 31) {
  250. while (n--) {
  251. audit_put_chunk(q->c[n]);
  252. q->c[n] = NULL;
  253. }
  254. }
  255. while (n-- > ctx->tree_count) {
  256. audit_put_chunk(q->c[n]);
  257. q->c[n] = NULL;
  258. }
  259. ctx->trees = p;
  260. ctx->tree_count = count;
  261. #endif
  262. }
  263. static void free_tree_refs(struct audit_context *ctx)
  264. {
  265. struct audit_tree_refs *p, *q;
  266. for (p = ctx->first_trees; p; p = q) {
  267. q = p->next;
  268. kfree(p);
  269. }
  270. }
  271. static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
  272. {
  273. #ifdef CONFIG_AUDIT_TREE
  274. struct audit_tree_refs *p;
  275. int n;
  276. if (!tree)
  277. return 0;
  278. /* full ones */
  279. for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
  280. for (n = 0; n < 31; n++)
  281. if (audit_tree_match(p->c[n], tree))
  282. return 1;
  283. }
  284. /* partial */
  285. if (p) {
  286. for (n = ctx->tree_count; n < 31; n++)
  287. if (audit_tree_match(p->c[n], tree))
  288. return 1;
  289. }
  290. #endif
  291. return 0;
  292. }
  293. static int audit_compare_uid(kuid_t uid,
  294. struct audit_names *name,
  295. struct audit_field *f,
  296. struct audit_context *ctx)
  297. {
  298. struct audit_names *n;
  299. int rc;
  300. if (name) {
  301. rc = audit_uid_comparator(uid, f->op, name->uid);
  302. if (rc)
  303. return rc;
  304. }
  305. if (ctx) {
  306. list_for_each_entry(n, &ctx->names_list, list) {
  307. rc = audit_uid_comparator(uid, f->op, n->uid);
  308. if (rc)
  309. return rc;
  310. }
  311. }
  312. return 0;
  313. }
  314. static int audit_compare_gid(kgid_t gid,
  315. struct audit_names *name,
  316. struct audit_field *f,
  317. struct audit_context *ctx)
  318. {
  319. struct audit_names *n;
  320. int rc;
  321. if (name) {
  322. rc = audit_gid_comparator(gid, f->op, name->gid);
  323. if (rc)
  324. return rc;
  325. }
  326. if (ctx) {
  327. list_for_each_entry(n, &ctx->names_list, list) {
  328. rc = audit_gid_comparator(gid, f->op, n->gid);
  329. if (rc)
  330. return rc;
  331. }
  332. }
  333. return 0;
  334. }
  335. static int audit_field_compare(struct task_struct *tsk,
  336. const struct cred *cred,
  337. struct audit_field *f,
  338. struct audit_context *ctx,
  339. struct audit_names *name)
  340. {
  341. switch (f->val) {
  342. /* process to file object comparisons */
  343. case AUDIT_COMPARE_UID_TO_OBJ_UID:
  344. return audit_compare_uid(cred->uid, name, f, ctx);
  345. case AUDIT_COMPARE_GID_TO_OBJ_GID:
  346. return audit_compare_gid(cred->gid, name, f, ctx);
  347. case AUDIT_COMPARE_EUID_TO_OBJ_UID:
  348. return audit_compare_uid(cred->euid, name, f, ctx);
  349. case AUDIT_COMPARE_EGID_TO_OBJ_GID:
  350. return audit_compare_gid(cred->egid, name, f, ctx);
  351. case AUDIT_COMPARE_AUID_TO_OBJ_UID:
  352. return audit_compare_uid(tsk->loginuid, name, f, ctx);
  353. case AUDIT_COMPARE_SUID_TO_OBJ_UID:
  354. return audit_compare_uid(cred->suid, name, f, ctx);
  355. case AUDIT_COMPARE_SGID_TO_OBJ_GID:
  356. return audit_compare_gid(cred->sgid, name, f, ctx);
  357. case AUDIT_COMPARE_FSUID_TO_OBJ_UID:
  358. return audit_compare_uid(cred->fsuid, name, f, ctx);
  359. case AUDIT_COMPARE_FSGID_TO_OBJ_GID:
  360. return audit_compare_gid(cred->fsgid, name, f, ctx);
  361. /* uid comparisons */
  362. case AUDIT_COMPARE_UID_TO_AUID:
  363. return audit_uid_comparator(cred->uid, f->op, tsk->loginuid);
  364. case AUDIT_COMPARE_UID_TO_EUID:
  365. return audit_uid_comparator(cred->uid, f->op, cred->euid);
  366. case AUDIT_COMPARE_UID_TO_SUID:
  367. return audit_uid_comparator(cred->uid, f->op, cred->suid);
  368. case AUDIT_COMPARE_UID_TO_FSUID:
  369. return audit_uid_comparator(cred->uid, f->op, cred->fsuid);
  370. /* auid comparisons */
  371. case AUDIT_COMPARE_AUID_TO_EUID:
  372. return audit_uid_comparator(tsk->loginuid, f->op, cred->euid);
  373. case AUDIT_COMPARE_AUID_TO_SUID:
  374. return audit_uid_comparator(tsk->loginuid, f->op, cred->suid);
  375. case AUDIT_COMPARE_AUID_TO_FSUID:
  376. return audit_uid_comparator(tsk->loginuid, f->op, cred->fsuid);
  377. /* euid comparisons */
  378. case AUDIT_COMPARE_EUID_TO_SUID:
  379. return audit_uid_comparator(cred->euid, f->op, cred->suid);
  380. case AUDIT_COMPARE_EUID_TO_FSUID:
  381. return audit_uid_comparator(cred->euid, f->op, cred->fsuid);
  382. /* suid comparisons */
  383. case AUDIT_COMPARE_SUID_TO_FSUID:
  384. return audit_uid_comparator(cred->suid, f->op, cred->fsuid);
  385. /* gid comparisons */
  386. case AUDIT_COMPARE_GID_TO_EGID:
  387. return audit_gid_comparator(cred->gid, f->op, cred->egid);
  388. case AUDIT_COMPARE_GID_TO_SGID:
  389. return audit_gid_comparator(cred->gid, f->op, cred->sgid);
  390. case AUDIT_COMPARE_GID_TO_FSGID:
  391. return audit_gid_comparator(cred->gid, f->op, cred->fsgid);
  392. /* egid comparisons */
  393. case AUDIT_COMPARE_EGID_TO_SGID:
  394. return audit_gid_comparator(cred->egid, f->op, cred->sgid);
  395. case AUDIT_COMPARE_EGID_TO_FSGID:
  396. return audit_gid_comparator(cred->egid, f->op, cred->fsgid);
  397. /* sgid comparison */
  398. case AUDIT_COMPARE_SGID_TO_FSGID:
  399. return audit_gid_comparator(cred->sgid, f->op, cred->fsgid);
  400. default:
  401. WARN(1, "Missing AUDIT_COMPARE define. Report as a bug\n");
  402. return 0;
  403. }
  404. return 0;
  405. }
  406. /* Determine if any context name data matches a rule's watch data */
  407. /* Compare a task_struct with an audit_rule. Return 1 on match, 0
  408. * otherwise.
  409. *
  410. * If task_creation is true, this is an explicit indication that we are
  411. * filtering a task rule at task creation time. This and tsk == current are
  412. * the only situations where tsk->cred may be accessed without an rcu read lock.
  413. */
  414. static int audit_filter_rules(struct task_struct *tsk,
  415. struct audit_krule *rule,
  416. struct audit_context *ctx,
  417. struct audit_names *name,
  418. enum audit_state *state,
  419. bool task_creation)
  420. {
  421. const struct cred *cred;
  422. int i, need_sid = 1;
  423. u32 sid;
  424. cred = rcu_dereference_check(tsk->cred, tsk == current || task_creation);
  425. for (i = 0; i < rule->field_count; i++) {
  426. struct audit_field *f = &rule->fields[i];
  427. struct audit_names *n;
  428. int result = 0;
  429. switch (f->type) {
  430. case AUDIT_PID:
  431. result = audit_comparator(tsk->pid, f->op, f->val);
  432. break;
  433. case AUDIT_PPID:
  434. if (ctx) {
  435. if (!ctx->ppid)
  436. ctx->ppid = sys_getppid();
  437. result = audit_comparator(ctx->ppid, f->op, f->val);
  438. }
  439. break;
  440. case AUDIT_UID:
  441. result = audit_uid_comparator(cred->uid, f->op, f->uid);
  442. break;
  443. case AUDIT_EUID:
  444. result = audit_uid_comparator(cred->euid, f->op, f->uid);
  445. break;
  446. case AUDIT_SUID:
  447. result = audit_uid_comparator(cred->suid, f->op, f->uid);
  448. break;
  449. case AUDIT_FSUID:
  450. result = audit_uid_comparator(cred->fsuid, f->op, f->uid);
  451. break;
  452. case AUDIT_GID:
  453. result = audit_gid_comparator(cred->gid, f->op, f->gid);
  454. if (f->op == Audit_equal) {
  455. if (!result)
  456. result = in_group_p(f->gid);
  457. } else if (f->op == Audit_not_equal) {
  458. if (result)
  459. result = !in_group_p(f->gid);
  460. }
  461. break;
  462. case AUDIT_EGID:
  463. result = audit_gid_comparator(cred->egid, f->op, f->gid);
  464. if (f->op == Audit_equal) {
  465. if (!result)
  466. result = in_egroup_p(f->gid);
  467. } else if (f->op == Audit_not_equal) {
  468. if (result)
  469. result = !in_egroup_p(f->gid);
  470. }
  471. break;
  472. case AUDIT_SGID:
  473. result = audit_gid_comparator(cred->sgid, f->op, f->gid);
  474. break;
  475. case AUDIT_FSGID:
  476. result = audit_gid_comparator(cred->fsgid, f->op, f->gid);
  477. break;
  478. case AUDIT_PERS:
  479. result = audit_comparator(tsk->personality, f->op, f->val);
  480. break;
  481. case AUDIT_ARCH:
  482. if (ctx)
  483. result = audit_comparator(ctx->arch, f->op, f->val);
  484. break;
  485. case AUDIT_EXIT:
  486. if (ctx && ctx->return_valid)
  487. result = audit_comparator(ctx->return_code, f->op, f->val);
  488. break;
  489. case AUDIT_SUCCESS:
  490. if (ctx && ctx->return_valid) {
  491. if (f->val)
  492. result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
  493. else
  494. result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
  495. }
  496. break;
  497. case AUDIT_DEVMAJOR:
  498. if (name) {
  499. if (audit_comparator(MAJOR(name->dev), f->op, f->val) ||
  500. audit_comparator(MAJOR(name->rdev), f->op, f->val))
  501. ++result;
  502. } else if (ctx) {
  503. list_for_each_entry(n, &ctx->names_list, list) {
  504. if (audit_comparator(MAJOR(n->dev), f->op, f->val) ||
  505. audit_comparator(MAJOR(n->rdev), f->op, f->val)) {
  506. ++result;
  507. break;
  508. }
  509. }
  510. }
  511. break;
  512. case AUDIT_DEVMINOR:
  513. if (name) {
  514. if (audit_comparator(MINOR(name->dev), f->op, f->val) ||
  515. audit_comparator(MINOR(name->rdev), f->op, f->val))
  516. ++result;
  517. } else if (ctx) {
  518. list_for_each_entry(n, &ctx->names_list, list) {
  519. if (audit_comparator(MINOR(n->dev), f->op, f->val) ||
  520. audit_comparator(MINOR(n->rdev), f->op, f->val)) {
  521. ++result;
  522. break;
  523. }
  524. }
  525. }
  526. break;
  527. case AUDIT_INODE:
  528. if (name)
  529. result = (name->ino == f->val);
  530. else if (ctx) {
  531. list_for_each_entry(n, &ctx->names_list, list) {
  532. if (audit_comparator(n->ino, f->op, f->val)) {
  533. ++result;
  534. break;
  535. }
  536. }
  537. }
  538. break;
  539. case AUDIT_OBJ_UID:
  540. if (name) {
  541. result = audit_uid_comparator(name->uid, f->op, f->uid);
  542. } else if (ctx) {
  543. list_for_each_entry(n, &ctx->names_list, list) {
  544. if (audit_uid_comparator(n->uid, f->op, f->uid)) {
  545. ++result;
  546. break;
  547. }
  548. }
  549. }
  550. break;
  551. case AUDIT_OBJ_GID:
  552. if (name) {
  553. result = audit_gid_comparator(name->gid, f->op, f->gid);
  554. } else if (ctx) {
  555. list_for_each_entry(n, &ctx->names_list, list) {
  556. if (audit_gid_comparator(n->gid, f->op, f->gid)) {
  557. ++result;
  558. break;
  559. }
  560. }
  561. }
  562. break;
  563. case AUDIT_WATCH:
  564. if (name)
  565. result = audit_watch_compare(rule->watch, name->ino, name->dev);
  566. break;
  567. case AUDIT_DIR:
  568. if (ctx)
  569. result = match_tree_refs(ctx, rule->tree);
  570. break;
  571. case AUDIT_LOGINUID:
  572. result = 0;
  573. if (ctx)
  574. result = audit_uid_comparator(tsk->loginuid, f->op, f->uid);
  575. break;
  576. case AUDIT_LOGINUID_SET:
  577. result = audit_comparator(audit_loginuid_set(tsk), f->op, f->val);
  578. break;
  579. case AUDIT_SUBJ_USER:
  580. case AUDIT_SUBJ_ROLE:
  581. case AUDIT_SUBJ_TYPE:
  582. case AUDIT_SUBJ_SEN:
  583. case AUDIT_SUBJ_CLR:
  584. /* NOTE: this may return negative values indicating
  585. a temporary error. We simply treat this as a
  586. match for now to avoid losing information that
  587. may be wanted. An error message will also be
  588. logged upon error */
  589. if (f->lsm_rule) {
  590. if (need_sid) {
  591. security_task_getsecid(tsk, &sid);
  592. need_sid = 0;
  593. }
  594. result = security_audit_rule_match(sid, f->type,
  595. f->op,
  596. f->lsm_rule,
  597. ctx);
  598. }
  599. break;
  600. case AUDIT_OBJ_USER:
  601. case AUDIT_OBJ_ROLE:
  602. case AUDIT_OBJ_TYPE:
  603. case AUDIT_OBJ_LEV_LOW:
  604. case AUDIT_OBJ_LEV_HIGH:
  605. /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
  606. also applies here */
  607. if (f->lsm_rule) {
  608. /* Find files that match */
  609. if (name) {
  610. result = security_audit_rule_match(
  611. name->osid, f->type, f->op,
  612. f->lsm_rule, ctx);
  613. } else if (ctx) {
  614. list_for_each_entry(n, &ctx->names_list, list) {
  615. if (security_audit_rule_match(n->osid, f->type,
  616. f->op, f->lsm_rule,
  617. ctx)) {
  618. ++result;
  619. break;
  620. }
  621. }
  622. }
  623. /* Find ipc objects that match */
  624. if (!ctx || ctx->type != AUDIT_IPC)
  625. break;
  626. if (security_audit_rule_match(ctx->ipc.osid,
  627. f->type, f->op,
  628. f->lsm_rule, ctx))
  629. ++result;
  630. }
  631. break;
  632. case AUDIT_ARG0:
  633. case AUDIT_ARG1:
  634. case AUDIT_ARG2:
  635. case AUDIT_ARG3:
  636. if (ctx)
  637. result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
  638. break;
  639. case AUDIT_FILTERKEY:
  640. /* ignore this field for filtering */
  641. result = 1;
  642. break;
  643. case AUDIT_PERM:
  644. result = audit_match_perm(ctx, f->val);
  645. break;
  646. case AUDIT_FILETYPE:
  647. result = audit_match_filetype(ctx, f->val);
  648. break;
  649. case AUDIT_FIELD_COMPARE:
  650. result = audit_field_compare(tsk, cred, f, ctx, name);
  651. break;
  652. }
  653. if (!result)
  654. return 0;
  655. }
  656. if (ctx) {
  657. if (rule->prio <= ctx->prio)
  658. return 0;
  659. if (rule->filterkey) {
  660. kfree(ctx->filterkey);
  661. ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
  662. }
  663. ctx->prio = rule->prio;
  664. }
  665. switch (rule->action) {
  666. case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
  667. case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
  668. }
  669. return 1;
  670. }
  671. /* At process creation time, we can determine if system-call auditing is
  672. * completely disabled for this task. Since we only have the task
  673. * structure at this point, we can only check uid and gid.
  674. */
  675. static enum audit_state audit_filter_task(struct task_struct *tsk, char **key)
  676. {
  677. struct audit_entry *e;
  678. enum audit_state state;
  679. rcu_read_lock();
  680. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
  681. if (audit_filter_rules(tsk, &e->rule, NULL, NULL,
  682. &state, true)) {
  683. if (state == AUDIT_RECORD_CONTEXT)
  684. *key = kstrdup(e->rule.filterkey, GFP_ATOMIC);
  685. rcu_read_unlock();
  686. return state;
  687. }
  688. }
  689. rcu_read_unlock();
  690. return AUDIT_BUILD_CONTEXT;
  691. }
  692. /* At syscall entry and exit time, this filter is called if the
  693. * audit_state is not low enough that auditing cannot take place, but is
  694. * also not high enough that we already know we have to write an audit
  695. * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
  696. */
  697. static enum audit_state audit_filter_syscall(struct task_struct *tsk,
  698. struct audit_context *ctx,
  699. struct list_head *list)
  700. {
  701. struct audit_entry *e;
  702. enum audit_state state;
  703. if (audit_pid && tsk->tgid == audit_pid)
  704. return AUDIT_DISABLED;
  705. rcu_read_lock();
  706. if (!list_empty(list)) {
  707. int word = AUDIT_WORD(ctx->major);
  708. int bit = AUDIT_BIT(ctx->major);
  709. list_for_each_entry_rcu(e, list, list) {
  710. if ((e->rule.mask[word] & bit) == bit &&
  711. audit_filter_rules(tsk, &e->rule, ctx, NULL,
  712. &state, false)) {
  713. rcu_read_unlock();
  714. ctx->current_state = state;
  715. return state;
  716. }
  717. }
  718. }
  719. rcu_read_unlock();
  720. return AUDIT_BUILD_CONTEXT;
  721. }
  722. /*
  723. * Given an audit_name check the inode hash table to see if they match.
  724. * Called holding the rcu read lock to protect the use of audit_inode_hash
  725. */
  726. static int audit_filter_inode_name(struct task_struct *tsk,
  727. struct audit_names *n,
  728. struct audit_context *ctx) {
  729. int word, bit;
  730. int h = audit_hash_ino((u32)n->ino);
  731. struct list_head *list = &audit_inode_hash[h];
  732. struct audit_entry *e;
  733. enum audit_state state;
  734. word = AUDIT_WORD(ctx->major);
  735. bit = AUDIT_BIT(ctx->major);
  736. if (list_empty(list))
  737. return 0;
  738. list_for_each_entry_rcu(e, list, list) {
  739. if ((e->rule.mask[word] & bit) == bit &&
  740. audit_filter_rules(tsk, &e->rule, ctx, n, &state, false)) {
  741. ctx->current_state = state;
  742. return 1;
  743. }
  744. }
  745. return 0;
  746. }
  747. /* At syscall exit time, this filter is called if any audit_names have been
  748. * collected during syscall processing. We only check rules in sublists at hash
  749. * buckets applicable to the inode numbers in audit_names.
  750. * Regarding audit_state, same rules apply as for audit_filter_syscall().
  751. */
  752. void audit_filter_inodes(struct task_struct *tsk, struct audit_context *ctx)
  753. {
  754. struct audit_names *n;
  755. if (audit_pid && tsk->tgid == audit_pid)
  756. return;
  757. rcu_read_lock();
  758. list_for_each_entry(n, &ctx->names_list, list) {
  759. if (audit_filter_inode_name(tsk, n, ctx))
  760. break;
  761. }
  762. rcu_read_unlock();
  763. }
  764. static inline struct audit_context *audit_get_context(struct task_struct *tsk,
  765. int return_valid,
  766. long return_code)
  767. {
  768. struct audit_context *context = tsk->audit_context;
  769. if (!context)
  770. return NULL;
  771. context->return_valid = return_valid;
  772. /*
  773. * we need to fix up the return code in the audit logs if the actual
  774. * return codes are later going to be fixed up by the arch specific
  775. * signal handlers
  776. *
  777. * This is actually a test for:
  778. * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
  779. * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
  780. *
  781. * but is faster than a bunch of ||
  782. */
  783. if (unlikely(return_code <= -ERESTARTSYS) &&
  784. (return_code >= -ERESTART_RESTARTBLOCK) &&
  785. (return_code != -ENOIOCTLCMD))
  786. context->return_code = -EINTR;
  787. else
  788. context->return_code = return_code;
  789. if (context->in_syscall && !context->dummy) {
  790. audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
  791. audit_filter_inodes(tsk, context);
  792. }
  793. tsk->audit_context = NULL;
  794. return context;
  795. }
  796. static inline void audit_free_names(struct audit_context *context)
  797. {
  798. struct audit_names *n, *next;
  799. #if AUDIT_DEBUG == 2
  800. if (context->put_count + context->ino_count != context->name_count) {
  801. int i = 0;
  802. printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
  803. " name_count=%d put_count=%d"
  804. " ino_count=%d [NOT freeing]\n",
  805. __FILE__, __LINE__,
  806. context->serial, context->major, context->in_syscall,
  807. context->name_count, context->put_count,
  808. context->ino_count);
  809. list_for_each_entry(n, &context->names_list, list) {
  810. printk(KERN_ERR "names[%d] = %p = %s\n", i++,
  811. n->name, n->name->name ?: "(null)");
  812. }
  813. dump_stack();
  814. return;
  815. }
  816. #endif
  817. #if AUDIT_DEBUG
  818. context->put_count = 0;
  819. context->ino_count = 0;
  820. #endif
  821. list_for_each_entry_safe(n, next, &context->names_list, list) {
  822. list_del(&n->list);
  823. if (n->name && n->name_put)
  824. final_putname(n->name);
  825. if (n->should_free)
  826. kfree(n);
  827. }
  828. context->name_count = 0;
  829. path_put(&context->pwd);
  830. context->pwd.dentry = NULL;
  831. context->pwd.mnt = NULL;
  832. }
  833. static inline void audit_free_aux(struct audit_context *context)
  834. {
  835. struct audit_aux_data *aux;
  836. while ((aux = context->aux)) {
  837. context->aux = aux->next;
  838. kfree(aux);
  839. }
  840. while ((aux = context->aux_pids)) {
  841. context->aux_pids = aux->next;
  842. kfree(aux);
  843. }
  844. }
  845. static inline struct audit_context *audit_alloc_context(enum audit_state state)
  846. {
  847. struct audit_context *context;
  848. context = kzalloc(sizeof(*context), GFP_KERNEL);
  849. if (!context)
  850. return NULL;
  851. context->state = state;
  852. context->prio = state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
  853. INIT_LIST_HEAD(&context->killed_trees);
  854. INIT_LIST_HEAD(&context->names_list);
  855. return context;
  856. }
  857. /**
  858. * audit_alloc - allocate an audit context block for a task
  859. * @tsk: task
  860. *
  861. * Filter on the task information and allocate a per-task audit context
  862. * if necessary. Doing so turns on system call auditing for the
  863. * specified task. This is called from copy_process, so no lock is
  864. * needed.
  865. */
  866. int audit_alloc(struct task_struct *tsk)
  867. {
  868. struct audit_context *context;
  869. enum audit_state state;
  870. char *key = NULL;
  871. if (likely(!audit_ever_enabled))
  872. return 0; /* Return if not auditing. */
  873. state = audit_filter_task(tsk, &key);
  874. if (state == AUDIT_DISABLED) {
  875. clear_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
  876. return 0;
  877. }
  878. if (!(context = audit_alloc_context(state))) {
  879. kfree(key);
  880. audit_log_lost("out of memory in audit_alloc");
  881. return -ENOMEM;
  882. }
  883. context->filterkey = key;
  884. tsk->audit_context = context;
  885. set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
  886. return 0;
  887. }
  888. static inline void audit_free_context(struct audit_context *context)
  889. {
  890. audit_free_names(context);
  891. unroll_tree_refs(context, NULL, 0);
  892. free_tree_refs(context);
  893. audit_free_aux(context);
  894. kfree(context->filterkey);
  895. kfree(context->sockaddr);
  896. kfree(context);
  897. }
  898. static int audit_log_pid_context(struct audit_context *context, pid_t pid,
  899. kuid_t auid, kuid_t uid, unsigned int sessionid,
  900. u32 sid, char *comm)
  901. {
  902. struct audit_buffer *ab;
  903. char *ctx = NULL;
  904. u32 len;
  905. int rc = 0;
  906. ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
  907. if (!ab)
  908. return rc;
  909. audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid,
  910. from_kuid(&init_user_ns, auid),
  911. from_kuid(&init_user_ns, uid), sessionid);
  912. if (sid) {
  913. if (security_secid_to_secctx(sid, &ctx, &len)) {
  914. audit_log_format(ab, " obj=(none)");
  915. rc = 1;
  916. } else {
  917. audit_log_format(ab, " obj=%s", ctx);
  918. security_release_secctx(ctx, len);
  919. }
  920. }
  921. audit_log_format(ab, " ocomm=");
  922. audit_log_untrustedstring(ab, comm);
  923. audit_log_end(ab);
  924. return rc;
  925. }
  926. /*
  927. * to_send and len_sent accounting are very loose estimates. We aren't
  928. * really worried about a hard cap to MAX_EXECVE_AUDIT_LEN so much as being
  929. * within about 500 bytes (next page boundary)
  930. *
  931. * why snprintf? an int is up to 12 digits long. if we just assumed when
  932. * logging that a[%d]= was going to be 16 characters long we would be wasting
  933. * space in every audit message. In one 7500 byte message we can log up to
  934. * about 1000 min size arguments. That comes down to about 50% waste of space
  935. * if we didn't do the snprintf to find out how long arg_num_len was.
  936. */
  937. static int audit_log_single_execve_arg(struct audit_context *context,
  938. struct audit_buffer **ab,
  939. int arg_num,
  940. size_t *len_sent,
  941. const char __user *p,
  942. char *buf)
  943. {
  944. char arg_num_len_buf[12];
  945. const char __user *tmp_p = p;
  946. /* how many digits are in arg_num? 5 is the length of ' a=""' */
  947. size_t arg_num_len = snprintf(arg_num_len_buf, 12, "%d", arg_num) + 5;
  948. size_t len, len_left, to_send;
  949. size_t max_execve_audit_len = MAX_EXECVE_AUDIT_LEN;
  950. unsigned int i, has_cntl = 0, too_long = 0;
  951. int ret;
  952. /* strnlen_user includes the null we don't want to send */
  953. len_left = len = strnlen_user(p, MAX_ARG_STRLEN) - 1;
  954. /*
  955. * We just created this mm, if we can't find the strings
  956. * we just copied into it something is _very_ wrong. Similar
  957. * for strings that are too long, we should not have created
  958. * any.
  959. */
  960. if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
  961. WARN_ON(1);
  962. send_sig(SIGKILL, current, 0);
  963. return -1;
  964. }
  965. /* walk the whole argument looking for non-ascii chars */
  966. do {
  967. if (len_left > MAX_EXECVE_AUDIT_LEN)
  968. to_send = MAX_EXECVE_AUDIT_LEN;
  969. else
  970. to_send = len_left;
  971. ret = copy_from_user(buf, tmp_p, to_send);
  972. /*
  973. * There is no reason for this copy to be short. We just
  974. * copied them here, and the mm hasn't been exposed to user-
  975. * space yet.
  976. */
  977. if (ret) {
  978. WARN_ON(1);
  979. send_sig(SIGKILL, current, 0);
  980. return -1;
  981. }
  982. buf[to_send] = '\0';
  983. has_cntl = audit_string_contains_control(buf, to_send);
  984. if (has_cntl) {
  985. /*
  986. * hex messages get logged as 2 bytes, so we can only
  987. * send half as much in each message
  988. */
  989. max_execve_audit_len = MAX_EXECVE_AUDIT_LEN / 2;
  990. break;
  991. }
  992. len_left -= to_send;
  993. tmp_p += to_send;
  994. } while (len_left > 0);
  995. len_left = len;
  996. if (len > max_execve_audit_len)
  997. too_long = 1;
  998. /* rewalk the argument actually logging the message */
  999. for (i = 0; len_left > 0; i++) {
  1000. int room_left;
  1001. if (len_left > max_execve_audit_len)
  1002. to_send = max_execve_audit_len;
  1003. else
  1004. to_send = len_left;
  1005. /* do we have space left to send this argument in this ab? */
  1006. room_left = MAX_EXECVE_AUDIT_LEN - arg_num_len - *len_sent;
  1007. if (has_cntl)
  1008. room_left -= (to_send * 2);
  1009. else
  1010. room_left -= to_send;
  1011. if (room_left < 0) {
  1012. *len_sent = 0;
  1013. audit_log_end(*ab);
  1014. *ab = audit_log_start(context, GFP_KERNEL, AUDIT_EXECVE);
  1015. if (!*ab)
  1016. return 0;
  1017. }
  1018. /*
  1019. * first record needs to say how long the original string was
  1020. * so we can be sure nothing was lost.
  1021. */
  1022. if ((i == 0) && (too_long))
  1023. audit_log_format(*ab, " a%d_len=%zu", arg_num,
  1024. has_cntl ? 2*len : len);
  1025. /*
  1026. * normally arguments are small enough to fit and we already
  1027. * filled buf above when we checked for control characters
  1028. * so don't bother with another copy_from_user
  1029. */
  1030. if (len >= max_execve_audit_len)
  1031. ret = copy_from_user(buf, p, to_send);
  1032. else
  1033. ret = 0;
  1034. if (ret) {
  1035. WARN_ON(1);
  1036. send_sig(SIGKILL, current, 0);
  1037. return -1;
  1038. }
  1039. buf[to_send] = '\0';
  1040. /* actually log it */
  1041. audit_log_format(*ab, " a%d", arg_num);
  1042. if (too_long)
  1043. audit_log_format(*ab, "[%d]", i);
  1044. audit_log_format(*ab, "=");
  1045. if (has_cntl)
  1046. audit_log_n_hex(*ab, buf, to_send);
  1047. else
  1048. audit_log_string(*ab, buf);
  1049. p += to_send;
  1050. len_left -= to_send;
  1051. *len_sent += arg_num_len;
  1052. if (has_cntl)
  1053. *len_sent += to_send * 2;
  1054. else
  1055. *len_sent += to_send;
  1056. }
  1057. /* include the null we didn't log */
  1058. return len + 1;
  1059. }
  1060. static void audit_log_execve_info(struct audit_context *context,
  1061. struct audit_buffer **ab,
  1062. struct audit_aux_data_execve *axi)
  1063. {
  1064. int i, len;
  1065. size_t len_sent = 0;
  1066. const char __user *p;
  1067. char *buf;
  1068. if (axi->mm != current->mm)
  1069. return; /* execve failed, no additional info */
  1070. p = (const char __user *)axi->mm->arg_start;
  1071. audit_log_format(*ab, "argc=%d", axi->argc);
  1072. /*
  1073. * we need some kernel buffer to hold the userspace args. Just
  1074. * allocate one big one rather than allocating one of the right size
  1075. * for every single argument inside audit_log_single_execve_arg()
  1076. * should be <8k allocation so should be pretty safe.
  1077. */
  1078. buf = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
  1079. if (!buf) {
  1080. audit_panic("out of memory for argv string\n");
  1081. return;
  1082. }
  1083. for (i = 0; i < axi->argc; i++) {
  1084. len = audit_log_single_execve_arg(context, ab, i,
  1085. &len_sent, p, buf);
  1086. if (len <= 0)
  1087. break;
  1088. p += len;
  1089. }
  1090. kfree(buf);
  1091. }
  1092. static void show_special(struct audit_context *context, int *call_panic)
  1093. {
  1094. struct audit_buffer *ab;
  1095. int i;
  1096. ab = audit_log_start(context, GFP_KERNEL, context->type);
  1097. if (!ab)
  1098. return;
  1099. switch (context->type) {
  1100. case AUDIT_SOCKETCALL: {
  1101. int nargs = context->socketcall.nargs;
  1102. audit_log_format(ab, "nargs=%d", nargs);
  1103. for (i = 0; i < nargs; i++)
  1104. audit_log_format(ab, " a%d=%lx", i,
  1105. context->socketcall.args[i]);
  1106. break; }
  1107. case AUDIT_IPC: {
  1108. u32 osid = context->ipc.osid;
  1109. audit_log_format(ab, "ouid=%u ogid=%u mode=%#ho",
  1110. from_kuid(&init_user_ns, context->ipc.uid),
  1111. from_kgid(&init_user_ns, context->ipc.gid),
  1112. context->ipc.mode);
  1113. if (osid) {
  1114. char *ctx = NULL;
  1115. u32 len;
  1116. if (security_secid_to_secctx(osid, &ctx, &len)) {
  1117. audit_log_format(ab, " osid=%u", osid);
  1118. *call_panic = 1;
  1119. } else {
  1120. audit_log_format(ab, " obj=%s", ctx);
  1121. security_release_secctx(ctx, len);
  1122. }
  1123. }
  1124. if (context->ipc.has_perm) {
  1125. audit_log_end(ab);
  1126. ab = audit_log_start(context, GFP_KERNEL,
  1127. AUDIT_IPC_SET_PERM);
  1128. if (unlikely(!ab))
  1129. return;
  1130. audit_log_format(ab,
  1131. "qbytes=%lx ouid=%u ogid=%u mode=%#ho",
  1132. context->ipc.qbytes,
  1133. context->ipc.perm_uid,
  1134. context->ipc.perm_gid,
  1135. context->ipc.perm_mode);
  1136. }
  1137. break; }
  1138. case AUDIT_MQ_OPEN: {
  1139. audit_log_format(ab,
  1140. "oflag=0x%x mode=%#ho mq_flags=0x%lx mq_maxmsg=%ld "
  1141. "mq_msgsize=%ld mq_curmsgs=%ld",
  1142. context->mq_open.oflag, context->mq_open.mode,
  1143. context->mq_open.attr.mq_flags,
  1144. context->mq_open.attr.mq_maxmsg,
  1145. context->mq_open.attr.mq_msgsize,
  1146. context->mq_open.attr.mq_curmsgs);
  1147. break; }
  1148. case AUDIT_MQ_SENDRECV: {
  1149. audit_log_format(ab,
  1150. "mqdes=%d msg_len=%zd msg_prio=%u "
  1151. "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
  1152. context->mq_sendrecv.mqdes,
  1153. context->mq_sendrecv.msg_len,
  1154. context->mq_sendrecv.msg_prio,
  1155. context->mq_sendrecv.abs_timeout.tv_sec,
  1156. context->mq_sendrecv.abs_timeout.tv_nsec);
  1157. break; }
  1158. case AUDIT_MQ_NOTIFY: {
  1159. audit_log_format(ab, "mqdes=%d sigev_signo=%d",
  1160. context->mq_notify.mqdes,
  1161. context->mq_notify.sigev_signo);
  1162. break; }
  1163. case AUDIT_MQ_GETSETATTR: {
  1164. struct mq_attr *attr = &context->mq_getsetattr.mqstat;
  1165. audit_log_format(ab,
  1166. "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
  1167. "mq_curmsgs=%ld ",
  1168. context->mq_getsetattr.mqdes,
  1169. attr->mq_flags, attr->mq_maxmsg,
  1170. attr->mq_msgsize, attr->mq_curmsgs);
  1171. break; }
  1172. case AUDIT_CAPSET: {
  1173. audit_log_format(ab, "pid=%d", context->capset.pid);
  1174. audit_log_cap(ab, "cap_pi", &context->capset.cap.inheritable);
  1175. audit_log_cap(ab, "cap_pp", &context->capset.cap.permitted);
  1176. audit_log_cap(ab, "cap_pe", &context->capset.cap.effective);
  1177. break; }
  1178. case AUDIT_MMAP: {
  1179. audit_log_format(ab, "fd=%d flags=0x%x", context->mmap.fd,
  1180. context->mmap.flags);
  1181. break; }
  1182. }
  1183. audit_log_end(ab);
  1184. }
  1185. static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
  1186. {
  1187. int i, call_panic = 0;
  1188. struct audit_buffer *ab;
  1189. struct audit_aux_data *aux;
  1190. struct audit_names *n;
  1191. /* tsk == current */
  1192. context->personality = tsk->personality;
  1193. ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
  1194. if (!ab)
  1195. return; /* audit_panic has been called */
  1196. audit_log_format(ab, "arch=%x syscall=%d",
  1197. context->arch, context->major);
  1198. if (context->personality != PER_LINUX)
  1199. audit_log_format(ab, " per=%lx", context->personality);
  1200. if (context->return_valid)
  1201. audit_log_format(ab, " success=%s exit=%ld",
  1202. (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
  1203. context->return_code);
  1204. audit_log_format(ab,
  1205. " a0=%lx a1=%lx a2=%lx a3=%lx items=%d",
  1206. context->argv[0],
  1207. context->argv[1],
  1208. context->argv[2],
  1209. context->argv[3],
  1210. context->name_count);
  1211. audit_log_task_info(ab, tsk);
  1212. audit_log_key(ab, context->filterkey);
  1213. audit_log_end(ab);
  1214. for (aux = context->aux; aux; aux = aux->next) {
  1215. ab = audit_log_start(context, GFP_KERNEL, aux->type);
  1216. if (!ab)
  1217. continue; /* audit_panic has been called */
  1218. switch (aux->type) {
  1219. case AUDIT_EXECVE: {
  1220. struct audit_aux_data_execve *axi = (void *)aux;
  1221. audit_log_execve_info(context, &ab, axi);
  1222. break; }
  1223. case AUDIT_BPRM_FCAPS: {
  1224. struct audit_aux_data_bprm_fcaps *axs = (void *)aux;
  1225. audit_log_format(ab, "fver=%x", axs->fcap_ver);
  1226. audit_log_cap(ab, "fp", &axs->fcap.permitted);
  1227. audit_log_cap(ab, "fi", &axs->fcap.inheritable);
  1228. audit_log_format(ab, " fe=%d", axs->fcap.fE);
  1229. audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted);
  1230. audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable);
  1231. audit_log_cap(ab, "old_pe", &axs->old_pcap.effective);
  1232. audit_log_cap(ab, "new_pp", &axs->new_pcap.permitted);
  1233. audit_log_cap(ab, "new_pi", &axs->new_pcap.inheritable);
  1234. audit_log_cap(ab, "new_pe", &axs->new_pcap.effective);
  1235. break; }
  1236. }
  1237. audit_log_end(ab);
  1238. }
  1239. if (context->type)
  1240. show_special(context, &call_panic);
  1241. if (context->fds[0] >= 0) {
  1242. ab = audit_log_start(context, GFP_KERNEL, AUDIT_FD_PAIR);
  1243. if (ab) {
  1244. audit_log_format(ab, "fd0=%d fd1=%d",
  1245. context->fds[0], context->fds[1]);
  1246. audit_log_end(ab);
  1247. }
  1248. }
  1249. if (context->sockaddr_len) {
  1250. ab = audit_log_start(context, GFP_KERNEL, AUDIT_SOCKADDR);
  1251. if (ab) {
  1252. audit_log_format(ab, "saddr=");
  1253. audit_log_n_hex(ab, (void *)context->sockaddr,
  1254. context->sockaddr_len);
  1255. audit_log_end(ab);
  1256. }
  1257. }
  1258. for (aux = context->aux_pids; aux; aux = aux->next) {
  1259. struct audit_aux_data_pids *axs = (void *)aux;
  1260. for (i = 0; i < axs->pid_count; i++)
  1261. if (audit_log_pid_context(context, axs->target_pid[i],
  1262. axs->target_auid[i],
  1263. axs->target_uid[i],
  1264. axs->target_sessionid[i],
  1265. axs->target_sid[i],
  1266. axs->target_comm[i]))
  1267. call_panic = 1;
  1268. }
  1269. if (context->target_pid &&
  1270. audit_log_pid_context(context, context->target_pid,
  1271. context->target_auid, context->target_uid,
  1272. context->target_sessionid,
  1273. context->target_sid, context->target_comm))
  1274. call_panic = 1;
  1275. if (context->pwd.dentry && context->pwd.mnt) {
  1276. ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
  1277. if (ab) {
  1278. audit_log_d_path(ab, " cwd=", &context->pwd);
  1279. audit_log_end(ab);
  1280. }
  1281. }
  1282. i = 0;
  1283. list_for_each_entry(n, &context->names_list, list) {
  1284. if (n->hidden)
  1285. continue;
  1286. audit_log_name(context, n, NULL, i++, &call_panic);
  1287. }
  1288. /* Send end of event record to help user space know we are finished */
  1289. ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
  1290. if (ab)
  1291. audit_log_end(ab);
  1292. if (call_panic)
  1293. audit_panic("error converting sid to string");
  1294. }
  1295. /**
  1296. * audit_free - free a per-task audit context
  1297. * @tsk: task whose audit context block to free
  1298. *
  1299. * Called from copy_process and do_exit
  1300. */
  1301. void __audit_free(struct task_struct *tsk)
  1302. {
  1303. struct audit_context *context;
  1304. context = audit_get_context(tsk, 0, 0);
  1305. if (!context)
  1306. return;
  1307. /* Check for system calls that do not go through the exit
  1308. * function (e.g., exit_group), then free context block.
  1309. * We use GFP_ATOMIC here because we might be doing this
  1310. * in the context of the idle thread */
  1311. /* that can happen only if we are called from do_exit() */
  1312. if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
  1313. audit_log_exit(context, tsk);
  1314. if (!list_empty(&context->killed_trees))
  1315. audit_kill_trees(&context->killed_trees);
  1316. audit_free_context(context);
  1317. }
  1318. /**
  1319. * audit_syscall_entry - fill in an audit record at syscall entry
  1320. * @arch: architecture type
  1321. * @major: major syscall type (function)
  1322. * @a1: additional syscall register 1
  1323. * @a2: additional syscall register 2
  1324. * @a3: additional syscall register 3
  1325. * @a4: additional syscall register 4
  1326. *
  1327. * Fill in audit context at syscall entry. This only happens if the
  1328. * audit context was created when the task was created and the state or
  1329. * filters demand the audit context be built. If the state from the
  1330. * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
  1331. * then the record will be written at syscall exit time (otherwise, it
  1332. * will only be written if another part of the kernel requests that it
  1333. * be written).
  1334. */
  1335. void __audit_syscall_entry(int arch, int major,
  1336. unsigned long a1, unsigned long a2,
  1337. unsigned long a3, unsigned long a4)
  1338. {
  1339. struct task_struct *tsk = current;
  1340. struct audit_context *context = tsk->audit_context;
  1341. enum audit_state state;
  1342. if (!context)
  1343. return;
  1344. BUG_ON(context->in_syscall || context->name_count);
  1345. if (!audit_enabled)
  1346. return;
  1347. context->arch = arch;
  1348. context->major = major;
  1349. context->argv[0] = a1;
  1350. context->argv[1] = a2;
  1351. context->argv[2] = a3;
  1352. context->argv[3] = a4;
  1353. state = context->state;
  1354. context->dummy = !audit_n_rules;
  1355. if (!context->dummy && state == AUDIT_BUILD_CONTEXT) {
  1356. context->prio = 0;
  1357. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
  1358. }
  1359. if (state == AUDIT_DISABLED)
  1360. return;
  1361. context->serial = 0;
  1362. context->ctime = CURRENT_TIME;
  1363. context->in_syscall = 1;
  1364. context->current_state = state;
  1365. context->ppid = 0;
  1366. }
  1367. /**
  1368. * audit_syscall_exit - deallocate audit context after a system call
  1369. * @success: success value of the syscall
  1370. * @return_code: return value of the syscall
  1371. *
  1372. * Tear down after system call. If the audit context has been marked as
  1373. * auditable (either because of the AUDIT_RECORD_CONTEXT state from
  1374. * filtering, or because some other part of the kernel wrote an audit
  1375. * message), then write out the syscall information. In call cases,
  1376. * free the names stored from getname().
  1377. */
  1378. void __audit_syscall_exit(int success, long return_code)
  1379. {
  1380. struct task_struct *tsk = current;
  1381. struct audit_context *context;
  1382. if (success)
  1383. success = AUDITSC_SUCCESS;
  1384. else
  1385. success = AUDITSC_FAILURE;
  1386. context = audit_get_context(tsk, success, return_code);
  1387. if (!context)
  1388. return;
  1389. if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
  1390. audit_log_exit(context, tsk);
  1391. context->in_syscall = 0;
  1392. context->prio = context->state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
  1393. if (!list_empty(&context->killed_trees))
  1394. audit_kill_trees(&context->killed_trees);
  1395. audit_free_names(context);
  1396. unroll_tree_refs(context, NULL, 0);
  1397. audit_free_aux(context);
  1398. context->aux = NULL;
  1399. context->aux_pids = NULL;
  1400. context->target_pid = 0;
  1401. context->target_sid = 0;
  1402. context->sockaddr_len = 0;
  1403. context->type = 0;
  1404. context->fds[0] = -1;
  1405. if (context->state != AUDIT_RECORD_CONTEXT) {
  1406. kfree(context->filterkey);
  1407. context->filterkey = NULL;
  1408. }
  1409. tsk->audit_context = context;
  1410. }
  1411. static inline void handle_one(const struct inode *inode)
  1412. {
  1413. #ifdef CONFIG_AUDIT_TREE
  1414. struct audit_context *context;
  1415. struct audit_tree_refs *p;
  1416. struct audit_chunk *chunk;
  1417. int count;
  1418. if (likely(hlist_empty(&inode->i_fsnotify_marks)))
  1419. return;
  1420. context = current->audit_context;
  1421. p = context->trees;
  1422. count = context->tree_count;
  1423. rcu_read_lock();
  1424. chunk = audit_tree_lookup(inode);
  1425. rcu_read_unlock();
  1426. if (!chunk)
  1427. return;
  1428. if (likely(put_tree_ref(context, chunk)))
  1429. return;
  1430. if (unlikely(!grow_tree_refs(context))) {
  1431. printk(KERN_WARNING "out of memory, audit has lost a tree reference\n");
  1432. audit_set_auditable(context);
  1433. audit_put_chunk(chunk);
  1434. unroll_tree_refs(context, p, count);
  1435. return;
  1436. }
  1437. put_tree_ref(context, chunk);
  1438. #endif
  1439. }
  1440. static void handle_path(const struct dentry *dentry)
  1441. {
  1442. #ifdef CONFIG_AUDIT_TREE
  1443. struct audit_context *context;
  1444. struct audit_tree_refs *p;
  1445. const struct dentry *d, *parent;
  1446. struct audit_chunk *drop;
  1447. unsigned long seq;
  1448. int count;
  1449. context = current->audit_context;
  1450. p = context->trees;
  1451. count = context->tree_count;
  1452. retry:
  1453. drop = NULL;
  1454. d = dentry;
  1455. rcu_read_lock();
  1456. seq = read_seqbegin(&rename_lock);
  1457. for(;;) {
  1458. struct inode *inode = d->d_inode;
  1459. if (inode && unlikely(!hlist_empty(&inode->i_fsnotify_marks))) {
  1460. struct audit_chunk *chunk;
  1461. chunk = audit_tree_lookup(inode);
  1462. if (chunk) {
  1463. if (unlikely(!put_tree_ref(context, chunk))) {
  1464. drop = chunk;
  1465. break;
  1466. }
  1467. }
  1468. }
  1469. parent = d->d_parent;
  1470. if (parent == d)
  1471. break;
  1472. d = parent;
  1473. }
  1474. if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
  1475. rcu_read_unlock();
  1476. if (!drop) {
  1477. /* just a race with rename */
  1478. unroll_tree_refs(context, p, count);
  1479. goto retry;
  1480. }
  1481. audit_put_chunk(drop);
  1482. if (grow_tree_refs(context)) {
  1483. /* OK, got more space */
  1484. unroll_tree_refs(context, p, count);
  1485. goto retry;
  1486. }
  1487. /* too bad */
  1488. printk(KERN_WARNING
  1489. "out of memory, audit has lost a tree reference\n");
  1490. unroll_tree_refs(context, p, count);
  1491. audit_set_auditable(context);
  1492. return;
  1493. }
  1494. rcu_read_unlock();
  1495. #endif
  1496. }
  1497. static struct audit_names *audit_alloc_name(struct audit_context *context,
  1498. unsigned char type)
  1499. {
  1500. struct audit_names *aname;
  1501. if (context->name_count < AUDIT_NAMES) {
  1502. aname = &context->preallocated_names[context->name_count];
  1503. memset(aname, 0, sizeof(*aname));
  1504. } else {
  1505. aname = kzalloc(sizeof(*aname), GFP_NOFS);
  1506. if (!aname)
  1507. return NULL;
  1508. aname->should_free = true;
  1509. }
  1510. aname->ino = (unsigned long)-1;
  1511. aname->type = type;
  1512. list_add_tail(&aname->list, &context->names_list);
  1513. context->name_count++;
  1514. #if AUDIT_DEBUG
  1515. context->ino_count++;
  1516. #endif
  1517. return aname;
  1518. }
  1519. /**
  1520. * audit_reusename - fill out filename with info from existing entry
  1521. * @uptr: userland ptr to pathname
  1522. *
  1523. * Search the audit_names list for the current audit context. If there is an
  1524. * existing entry with a matching "uptr" then return the filename
  1525. * associated with that audit_name. If not, return NULL.
  1526. */
  1527. struct filename *
  1528. __audit_reusename(const __user char *uptr)
  1529. {
  1530. struct audit_context *context = current->audit_context;
  1531. struct audit_names *n;
  1532. list_for_each_entry(n, &context->names_list, list) {
  1533. if (!n->name)
  1534. continue;
  1535. if (n->name->uptr == uptr)
  1536. return n->name;
  1537. }
  1538. return NULL;
  1539. }
  1540. /**
  1541. * audit_getname - add a name to the list
  1542. * @name: name to add
  1543. *
  1544. * Add a name to the list of audit names for this context.
  1545. * Called from fs/namei.c:getname().
  1546. */
  1547. void __audit_getname(struct filename *name)
  1548. {
  1549. struct audit_context *context = current->audit_context;
  1550. struct audit_names *n;
  1551. if (!context->in_syscall) {
  1552. #if AUDIT_DEBUG == 2
  1553. printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
  1554. __FILE__, __LINE__, context->serial, name);
  1555. dump_stack();
  1556. #endif
  1557. return;
  1558. }
  1559. #if AUDIT_DEBUG
  1560. /* The filename _must_ have a populated ->name */
  1561. BUG_ON(!name->name);
  1562. #endif
  1563. n = audit_alloc_name(context, AUDIT_TYPE_UNKNOWN);
  1564. if (!n)
  1565. return;
  1566. n->name = name;
  1567. n->name_len = AUDIT_NAME_FULL;
  1568. n->name_put = true;
  1569. name->aname = n;
  1570. if (!context->pwd.dentry)
  1571. get_fs_pwd(current->fs, &context->pwd);
  1572. }
  1573. /* audit_putname - intercept a putname request
  1574. * @name: name to intercept and delay for putname
  1575. *
  1576. * If we have stored the name from getname in the audit context,
  1577. * then we delay the putname until syscall exit.
  1578. * Called from include/linux/fs.h:putname().
  1579. */
  1580. void audit_putname(struct filename *name)
  1581. {
  1582. struct audit_context *context = current->audit_context;
  1583. BUG_ON(!context);
  1584. if (!context->in_syscall) {
  1585. #if AUDIT_DEBUG == 2
  1586. printk(KERN_ERR "%s:%d(:%d): final_putname(%p)\n",
  1587. __FILE__, __LINE__, context->serial, name);
  1588. if (context->name_count) {
  1589. struct audit_names *n;
  1590. int i = 0;
  1591. list_for_each_entry(n, &context->names_list, list)
  1592. printk(KERN_ERR "name[%d] = %p = %s\n", i++,
  1593. n->name, n->name->name ?: "(null)");
  1594. }
  1595. #endif
  1596. final_putname(name);
  1597. }
  1598. #if AUDIT_DEBUG
  1599. else {
  1600. ++context->put_count;
  1601. if (context->put_count > context->name_count) {
  1602. printk(KERN_ERR "%s:%d(:%d): major=%d"
  1603. " in_syscall=%d putname(%p) name_count=%d"
  1604. " put_count=%d\n",
  1605. __FILE__, __LINE__,
  1606. context->serial, context->major,
  1607. context->in_syscall, name->name,
  1608. context->name_count, context->put_count);
  1609. dump_stack();
  1610. }
  1611. }
  1612. #endif
  1613. }
  1614. /**
  1615. * __audit_inode - store the inode and device from a lookup
  1616. * @name: name being audited
  1617. * @dentry: dentry being audited
  1618. * @flags: attributes for this particular entry
  1619. */
  1620. void __audit_inode(struct filename *name, const struct dentry *dentry,
  1621. unsigned int flags)
  1622. {
  1623. struct audit_context *context = current->audit_context;
  1624. const struct inode *inode = dentry->d_inode;
  1625. struct audit_names *n;
  1626. bool parent = flags & AUDIT_INODE_PARENT;
  1627. if (!context->in_syscall)
  1628. return;
  1629. if (!name)
  1630. goto out_alloc;
  1631. #if AUDIT_DEBUG
  1632. /* The struct filename _must_ have a populated ->name */
  1633. BUG_ON(!name->name);
  1634. #endif
  1635. /*
  1636. * If we have a pointer to an audit_names entry already, then we can
  1637. * just use it directly if the type is correct.
  1638. */
  1639. n = name->aname;
  1640. if (n) {
  1641. if (parent) {
  1642. if (n->type == AUDIT_TYPE_PARENT ||
  1643. n->type == AUDIT_TYPE_UNKNOWN)
  1644. goto out;
  1645. } else {
  1646. if (n->type != AUDIT_TYPE_PARENT)
  1647. goto out;
  1648. }
  1649. }
  1650. list_for_each_entry_reverse(n, &context->names_list, list) {
  1651. /* does the name pointer match? */
  1652. if (!n->name || n->name->name != name->name)
  1653. continue;
  1654. /* match the correct record type */
  1655. if (parent) {
  1656. if (n->type == AUDIT_TYPE_PARENT ||
  1657. n->type == AUDIT_TYPE_UNKNOWN)
  1658. goto out;
  1659. } else {
  1660. if (n->type != AUDIT_TYPE_PARENT)
  1661. goto out;
  1662. }
  1663. }
  1664. out_alloc:
  1665. /* unable to find the name from a previous getname(). Allocate a new
  1666. * anonymous entry.
  1667. */
  1668. n = audit_alloc_name(context, AUDIT_TYPE_NORMAL);
  1669. if (!n)
  1670. return;
  1671. out:
  1672. if (parent) {
  1673. n->name_len = n->name ? parent_len(n->name->name) : AUDIT_NAME_FULL;
  1674. n->type = AUDIT_TYPE_PARENT;
  1675. if (flags & AUDIT_INODE_HIDDEN)
  1676. n->hidden = true;
  1677. } else {
  1678. n->name_len = AUDIT_NAME_FULL;
  1679. n->type = AUDIT_TYPE_NORMAL;
  1680. }
  1681. handle_path(dentry);
  1682. audit_copy_inode(n, dentry, inode);
  1683. }
  1684. /**
  1685. * __audit_inode_child - collect inode info for created/removed objects
  1686. * @parent: inode of dentry parent
  1687. * @dentry: dentry being audited
  1688. * @type: AUDIT_TYPE_* value that we're looking for
  1689. *
  1690. * For syscalls that create or remove filesystem objects, audit_inode
  1691. * can only collect information for the filesystem object's parent.
  1692. * This call updates the audit context with the child's information.
  1693. * Syscalls that create a new filesystem object must be hooked after
  1694. * the object is created. Syscalls that remove a filesystem object
  1695. * must be hooked prior, in order to capture the target inode during
  1696. * unsuccessful attempts.
  1697. */
  1698. void __audit_inode_child(const struct inode *parent,
  1699. const struct dentry *dentry,
  1700. const unsigned char type)
  1701. {
  1702. struct audit_context *context = current->audit_context;
  1703. const struct inode *inode = dentry->d_inode;
  1704. const char *dname = dentry->d_name.name;
  1705. struct audit_names *n, *found_parent = NULL, *found_child = NULL;
  1706. if (!context->in_syscall)
  1707. return;
  1708. if (inode)
  1709. handle_one(inode);
  1710. /* look for a parent entry first */
  1711. list_for_each_entry(n, &context->names_list, list) {
  1712. if (!n->name || n->type != AUDIT_TYPE_PARENT)
  1713. continue;
  1714. if (n->ino == parent->i_ino &&
  1715. !audit_compare_dname_path(dname, n->name->name, n->name_len)) {
  1716. found_parent = n;
  1717. break;
  1718. }
  1719. }
  1720. /* is there a matching child entry? */
  1721. list_for_each_entry(n, &context->names_list, list) {
  1722. /* can only match entries that have a name */
  1723. if (!n->name || n->type != type)
  1724. continue;
  1725. /* if we found a parent, make sure this one is a child of it */
  1726. if (found_parent && (n->name != found_parent->name))
  1727. continue;
  1728. if (!strcmp(dname, n->name->name) ||
  1729. !audit_compare_dname_path(dname, n->name->name,
  1730. found_parent ?
  1731. found_parent->name_len :
  1732. AUDIT_NAME_FULL)) {
  1733. found_child = n;
  1734. break;
  1735. }
  1736. }
  1737. if (!found_parent) {
  1738. /* create a new, "anonymous" parent record */
  1739. n = audit_alloc_name(context, AUDIT_TYPE_PARENT);
  1740. if (!n)
  1741. return;
  1742. audit_copy_inode(n, NULL, parent);
  1743. }
  1744. if (!found_child) {
  1745. found_child = audit_alloc_name(context, type);
  1746. if (!found_child)
  1747. return;
  1748. /* Re-use the name belonging to the slot for a matching parent
  1749. * directory. All names for this context are relinquished in
  1750. * audit_free_names() */
  1751. if (found_parent) {
  1752. found_child->name = found_parent->name;
  1753. found_child->name_len = AUDIT_NAME_FULL;
  1754. /* don't call __putname() */
  1755. found_child->name_put = false;
  1756. }
  1757. }
  1758. if (inode)
  1759. audit_copy_inode(found_child, dentry, inode);
  1760. else
  1761. found_child->ino = (unsigned long)-1;
  1762. }
  1763. EXPORT_SYMBOL_GPL(__audit_inode_child);
  1764. /**
  1765. * auditsc_get_stamp - get local copies of audit_context values
  1766. * @ctx: audit_context for the task
  1767. * @t: timespec to store time recorded in the audit_context
  1768. * @serial: serial value that is recorded in the audit_context
  1769. *
  1770. * Also sets the context as auditable.
  1771. */
  1772. int auditsc_get_stamp(struct audit_context *ctx,
  1773. struct timespec *t, unsigned int *serial)
  1774. {
  1775. if (!ctx->in_syscall)
  1776. return 0;
  1777. if (!ctx->serial)
  1778. ctx->serial = audit_serial();
  1779. t->tv_sec = ctx->ctime.tv_sec;
  1780. t->tv_nsec = ctx->ctime.tv_nsec;
  1781. *serial = ctx->serial;
  1782. if (!ctx->prio) {
  1783. ctx->prio = 1;
  1784. ctx->current_state = AUDIT_RECORD_CONTEXT;
  1785. }
  1786. return 1;
  1787. }
  1788. /* global counter which is incremented every time something logs in */
  1789. static atomic_t session_id = ATOMIC_INIT(0);
  1790. static int audit_set_loginuid_perm(kuid_t loginuid)
  1791. {
  1792. /* if we are unset, we don't need privs */
  1793. if (!audit_loginuid_set(current))
  1794. return 0;
  1795. /* if AUDIT_FEATURE_LOGINUID_IMMUTABLE means never ever allow a change*/
  1796. if (is_audit_feature_set(AUDIT_FEATURE_LOGINUID_IMMUTABLE))
  1797. return -EPERM;
  1798. /* it is set, you need permission */
  1799. if (!capable(CAP_AUDIT_CONTROL))
  1800. return -EPERM;
  1801. /* reject if this is not an unset and we don't allow that */
  1802. if (is_audit_feature_set(AUDIT_FEATURE_ONLY_UNSET_LOGINUID) && uid_valid(loginuid))
  1803. return -EPERM;
  1804. return 0;
  1805. }
  1806. static void audit_log_set_loginuid(kuid_t koldloginuid, kuid_t kloginuid,
  1807. unsigned int oldsessionid, unsigned int sessionid,
  1808. int rc)
  1809. {
  1810. struct audit_buffer *ab;
  1811. uid_t uid, ologinuid, nloginuid;
  1812. uid = from_kuid(&init_user_ns, task_uid(current));
  1813. ologinuid = from_kuid(&init_user_ns, koldloginuid);
  1814. nloginuid = from_kuid(&init_user_ns, kloginuid),
  1815. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
  1816. if (!ab)
  1817. return;
  1818. audit_log_format(ab, "pid=%d uid=%u old auid=%u new auid=%u old "
  1819. "ses=%u new ses=%u res=%d", current->pid, uid, ologinuid,
  1820. nloginuid, oldsessionid, sessionid, !rc);
  1821. audit_log_end(ab);
  1822. }
  1823. /**
  1824. * audit_set_loginuid - set current task's audit_context loginuid
  1825. * @loginuid: loginuid value
  1826. *
  1827. * Returns 0.
  1828. *
  1829. * Called (set) from fs/proc/base.c::proc_loginuid_write().
  1830. */
  1831. int audit_set_loginuid(kuid_t loginuid)
  1832. {
  1833. struct task_struct *task = current;
  1834. unsigned int sessionid = -1;
  1835. kuid_t oldloginuid, oldsessionid;
  1836. int rc;
  1837. oldloginuid = audit_get_loginuid(current);
  1838. oldsessionid = audit_get_sessionid(current);
  1839. rc = audit_set_loginuid_perm(loginuid);
  1840. if (rc)
  1841. goto out;
  1842. /* are we setting or clearing? */
  1843. if (uid_valid(loginuid))
  1844. sessionid = atomic_inc_return(&session_id);
  1845. task->sessionid = sessionid;
  1846. task->loginuid = loginuid;
  1847. out:
  1848. audit_log_set_loginuid(oldloginuid, loginuid, oldsessionid, sessionid, rc);
  1849. return rc;
  1850. }
  1851. /**
  1852. * __audit_mq_open - record audit data for a POSIX MQ open
  1853. * @oflag: open flag
  1854. * @mode: mode bits
  1855. * @attr: queue attributes
  1856. *
  1857. */
  1858. void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
  1859. {
  1860. struct audit_context *context = current->audit_context;
  1861. if (attr)
  1862. memcpy(&context->mq_open.attr, attr, sizeof(struct mq_attr));
  1863. else
  1864. memset(&context->mq_open.attr, 0, sizeof(struct mq_attr));
  1865. context->mq_open.oflag = oflag;
  1866. context->mq_open.mode = mode;
  1867. context->type = AUDIT_MQ_OPEN;
  1868. }
  1869. /**
  1870. * __audit_mq_sendrecv - record audit data for a POSIX MQ timed send/receive
  1871. * @mqdes: MQ descriptor
  1872. * @msg_len: Message length
  1873. * @msg_prio: Message priority
  1874. * @abs_timeout: Message timeout in absolute time
  1875. *
  1876. */
  1877. void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
  1878. const struct timespec *abs_timeout)
  1879. {
  1880. struct audit_context *context = current->audit_context;
  1881. struct timespec *p = &context->mq_sendrecv.abs_timeout;
  1882. if (abs_timeout)
  1883. memcpy(p, abs_timeout, sizeof(struct timespec));
  1884. else
  1885. memset(p, 0, sizeof(struct timespec));
  1886. context->mq_sendrecv.mqdes = mqdes;
  1887. context->mq_sendrecv.msg_len = msg_len;
  1888. context->mq_sendrecv.msg_prio = msg_prio;
  1889. context->type = AUDIT_MQ_SENDRECV;
  1890. }
  1891. /**
  1892. * __audit_mq_notify - record audit data for a POSIX MQ notify
  1893. * @mqdes: MQ descriptor
  1894. * @notification: Notification event
  1895. *
  1896. */
  1897. void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
  1898. {
  1899. struct audit_context *context = current->audit_context;
  1900. if (notification)
  1901. context->mq_notify.sigev_signo = notification->sigev_signo;
  1902. else
  1903. context->mq_notify.sigev_signo = 0;
  1904. context->mq_notify.mqdes = mqdes;
  1905. context->type = AUDIT_MQ_NOTIFY;
  1906. }
  1907. /**
  1908. * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
  1909. * @mqdes: MQ descriptor
  1910. * @mqstat: MQ flags
  1911. *
  1912. */
  1913. void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
  1914. {
  1915. struct audit_context *context = current->audit_context;
  1916. context->mq_getsetattr.mqdes = mqdes;
  1917. context->mq_getsetattr.mqstat = *mqstat;
  1918. context->type = AUDIT_MQ_GETSETATTR;
  1919. }
  1920. /**
  1921. * audit_ipc_obj - record audit data for ipc object
  1922. * @ipcp: ipc permissions
  1923. *
  1924. */
  1925. void __audit_ipc_obj(struct kern_ipc_perm *ipcp)
  1926. {
  1927. struct audit_context *context = current->audit_context;
  1928. context->ipc.uid = ipcp->uid;
  1929. context->ipc.gid = ipcp->gid;
  1930. context->ipc.mode = ipcp->mode;
  1931. context->ipc.has_perm = 0;
  1932. security_ipc_getsecid(ipcp, &context->ipc.osid);
  1933. context->type = AUDIT_IPC;
  1934. }
  1935. /**
  1936. * audit_ipc_set_perm - record audit data for new ipc permissions
  1937. * @qbytes: msgq bytes
  1938. * @uid: msgq user id
  1939. * @gid: msgq group id
  1940. * @mode: msgq mode (permissions)
  1941. *
  1942. * Called only after audit_ipc_obj().
  1943. */
  1944. void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode)
  1945. {
  1946. struct audit_context *context = current->audit_context;
  1947. context->ipc.qbytes = qbytes;
  1948. context->ipc.perm_uid = uid;
  1949. context->ipc.perm_gid = gid;
  1950. context->ipc.perm_mode = mode;
  1951. context->ipc.has_perm = 1;
  1952. }
  1953. int __audit_bprm(struct linux_binprm *bprm)
  1954. {
  1955. struct audit_aux_data_execve *ax;
  1956. struct audit_context *context = current->audit_context;
  1957. ax = kmalloc(sizeof(*ax), GFP_KERNEL);
  1958. if (!ax)
  1959. return -ENOMEM;
  1960. ax->argc = bprm->argc;
  1961. ax->envc = bprm->envc;
  1962. ax->mm = bprm->mm;
  1963. ax->d.type = AUDIT_EXECVE;
  1964. ax->d.next = context->aux;
  1965. context->aux = (void *)ax;
  1966. return 0;
  1967. }
  1968. /**
  1969. * audit_socketcall - record audit data for sys_socketcall
  1970. * @nargs: number of args, which should not be more than AUDITSC_ARGS.
  1971. * @args: args array
  1972. *
  1973. */
  1974. int __audit_socketcall(int nargs, unsigned long *args)
  1975. {
  1976. struct audit_context *context = current->audit_context;
  1977. if (nargs <= 0 || nargs > AUDITSC_ARGS || !args)
  1978. return -EINVAL;
  1979. context->type = AUDIT_SOCKETCALL;
  1980. context->socketcall.nargs = nargs;
  1981. memcpy(context->socketcall.args, args, nargs * sizeof(unsigned long));
  1982. return 0;
  1983. }
  1984. /**
  1985. * __audit_fd_pair - record audit data for pipe and socketpair
  1986. * @fd1: the first file descriptor
  1987. * @fd2: the second file descriptor
  1988. *
  1989. */
  1990. void __audit_fd_pair(int fd1, int fd2)
  1991. {
  1992. struct audit_context *context = current->audit_context;
  1993. context->fds[0] = fd1;
  1994. context->fds[1] = fd2;
  1995. }
  1996. /**
  1997. * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
  1998. * @len: data length in user space
  1999. * @a: data address in kernel space
  2000. *
  2001. * Returns 0 for success or NULL context or < 0 on error.
  2002. */
  2003. int __audit_sockaddr(int len, void *a)
  2004. {
  2005. struct audit_context *context = current->audit_context;
  2006. if (!context->sockaddr) {
  2007. void *p = kmalloc(sizeof(struct sockaddr_storage), GFP_KERNEL);
  2008. if (!p)
  2009. return -ENOMEM;
  2010. context->sockaddr = p;
  2011. }
  2012. context->sockaddr_len = len;
  2013. memcpy(context->sockaddr, a, len);
  2014. return 0;
  2015. }
  2016. void __audit_ptrace(struct task_struct *t)
  2017. {
  2018. struct audit_context *context = current->audit_context;
  2019. context->target_pid = t->pid;
  2020. context->target_auid = audit_get_loginuid(t);
  2021. context->target_uid = task_uid(t);
  2022. context->target_sessionid = audit_get_sessionid(t);
  2023. security_task_getsecid(t, &context->target_sid);
  2024. memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
  2025. }
  2026. /**
  2027. * audit_signal_info - record signal info for shutting down audit subsystem
  2028. * @sig: signal value
  2029. * @t: task being signaled
  2030. *
  2031. * If the audit subsystem is being terminated, record the task (pid)
  2032. * and uid that is doing that.
  2033. */
  2034. int __audit_signal_info(int sig, struct task_struct *t)
  2035. {
  2036. struct audit_aux_data_pids *axp;
  2037. struct task_struct *tsk = current;
  2038. struct audit_context *ctx = tsk->audit_context;
  2039. kuid_t uid = current_uid(), t_uid = task_uid(t);
  2040. if (audit_pid && t->tgid == audit_pid) {
  2041. if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1 || sig == SIGUSR2) {
  2042. audit_sig_pid = tsk->pid;
  2043. if (uid_valid(tsk->loginuid))
  2044. audit_sig_uid = tsk->loginuid;
  2045. else
  2046. audit_sig_uid = uid;
  2047. security_task_getsecid(tsk, &audit_sig_sid);
  2048. }
  2049. if (!audit_signals || audit_dummy_context())
  2050. return 0;
  2051. }
  2052. /* optimize the common case by putting first signal recipient directly
  2053. * in audit_context */
  2054. if (!ctx->target_pid) {
  2055. ctx->target_pid = t->tgid;
  2056. ctx->target_auid = audit_get_loginuid(t);
  2057. ctx->target_uid = t_uid;
  2058. ctx->target_sessionid = audit_get_sessionid(t);
  2059. security_task_getsecid(t, &ctx->target_sid);
  2060. memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
  2061. return 0;
  2062. }
  2063. axp = (void *)ctx->aux_pids;
  2064. if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
  2065. axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
  2066. if (!axp)
  2067. return -ENOMEM;
  2068. axp->d.type = AUDIT_OBJ_PID;
  2069. axp->d.next = ctx->aux_pids;
  2070. ctx->aux_pids = (void *)axp;
  2071. }
  2072. BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
  2073. axp->target_pid[axp->pid_count] = t->tgid;
  2074. axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
  2075. axp->target_uid[axp->pid_count] = t_uid;
  2076. axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
  2077. security_task_getsecid(t, &axp->target_sid[axp->pid_count]);
  2078. memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
  2079. axp->pid_count++;
  2080. return 0;
  2081. }
  2082. /**
  2083. * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps
  2084. * @bprm: pointer to the bprm being processed
  2085. * @new: the proposed new credentials
  2086. * @old: the old credentials
  2087. *
  2088. * Simply check if the proc already has the caps given by the file and if not
  2089. * store the priv escalation info for later auditing at the end of the syscall
  2090. *
  2091. * -Eric
  2092. */
  2093. int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
  2094. const struct cred *new, const struct cred *old)
  2095. {
  2096. struct audit_aux_data_bprm_fcaps *ax;
  2097. struct audit_context *context = current->audit_context;
  2098. struct cpu_vfs_cap_data vcaps;
  2099. struct dentry *dentry;
  2100. ax = kmalloc(sizeof(*ax), GFP_KERNEL);
  2101. if (!ax)
  2102. return -ENOMEM;
  2103. ax->d.type = AUDIT_BPRM_FCAPS;
  2104. ax->d.next = context->aux;
  2105. context->aux = (void *)ax;
  2106. dentry = dget(bprm->file->f_dentry);
  2107. get_vfs_caps_from_disk(dentry, &vcaps);
  2108. dput(dentry);
  2109. ax->fcap.permitted = vcaps.permitted;
  2110. ax->fcap.inheritable = vcaps.inheritable;
  2111. ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
  2112. ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
  2113. ax->old_pcap.permitted = old->cap_permitted;
  2114. ax->old_pcap.inheritable = old->cap_inheritable;
  2115. ax->old_pcap.effective = old->cap_effective;
  2116. ax->new_pcap.permitted = new->cap_permitted;
  2117. ax->new_pcap.inheritable = new->cap_inheritable;
  2118. ax->new_pcap.effective = new->cap_effective;
  2119. return 0;
  2120. }
  2121. /**
  2122. * __audit_log_capset - store information about the arguments to the capset syscall
  2123. * @pid: target pid of the capset call
  2124. * @new: the new credentials
  2125. * @old: the old (current) credentials
  2126. *
  2127. * Record the aguments userspace sent to sys_capset for later printing by the
  2128. * audit system if applicable
  2129. */
  2130. void __audit_log_capset(pid_t pid,
  2131. const struct cred *new, const struct cred *old)
  2132. {
  2133. struct audit_context *context = current->audit_context;
  2134. context->capset.pid = pid;
  2135. context->capset.cap.effective = new->cap_effective;
  2136. context->capset.cap.inheritable = new->cap_effective;
  2137. context->capset.cap.permitted = new->cap_permitted;
  2138. context->type = AUDIT_CAPSET;
  2139. }
  2140. void __audit_mmap_fd(int fd, int flags)
  2141. {
  2142. struct audit_context *context = current->audit_context;
  2143. context->mmap.fd = fd;
  2144. context->mmap.flags = flags;
  2145. context->type = AUDIT_MMAP;
  2146. }
  2147. static void audit_log_task(struct audit_buffer *ab)
  2148. {
  2149. kuid_t auid, uid;
  2150. kgid_t gid;
  2151. unsigned int sessionid;
  2152. auid = audit_get_loginuid(current);
  2153. sessionid = audit_get_sessionid(current);
  2154. current_uid_gid(&uid, &gid);
  2155. audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
  2156. from_kuid(&init_user_ns, auid),
  2157. from_kuid(&init_user_ns, uid),
  2158. from_kgid(&init_user_ns, gid),
  2159. sessionid);
  2160. audit_log_task_context(ab);
  2161. audit_log_format(ab, " pid=%d comm=", current->pid);
  2162. audit_log_untrustedstring(ab, current->comm);
  2163. }
  2164. static void audit_log_abend(struct audit_buffer *ab, char *reason, long signr)
  2165. {
  2166. audit_log_task(ab);
  2167. audit_log_format(ab, " reason=");
  2168. audit_log_string(ab, reason);
  2169. audit_log_format(ab, " sig=%ld", signr);
  2170. }
  2171. /**
  2172. * audit_core_dumps - record information about processes that end abnormally
  2173. * @signr: signal value
  2174. *
  2175. * If a process ends with a core dump, something fishy is going on and we
  2176. * should record the event for investigation.
  2177. */
  2178. void audit_core_dumps(long signr)
  2179. {
  2180. struct audit_buffer *ab;
  2181. if (!audit_enabled)
  2182. return;
  2183. if (signr == SIGQUIT) /* don't care for those */
  2184. return;
  2185. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
  2186. if (unlikely(!ab))
  2187. return;
  2188. audit_log_abend(ab, "memory violation", signr);
  2189. audit_log_end(ab);
  2190. }
  2191. void __audit_seccomp(unsigned long syscall, long signr, int code)
  2192. {
  2193. struct audit_buffer *ab;
  2194. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_SECCOMP);
  2195. if (unlikely(!ab))
  2196. return;
  2197. audit_log_task(ab);
  2198. audit_log_format(ab, " sig=%ld", signr);
  2199. audit_log_format(ab, " syscall=%ld", syscall);
  2200. audit_log_format(ab, " compat=%d", is_compat_task());
  2201. audit_log_format(ab, " ip=0x%lx", KSTK_EIP(current));
  2202. audit_log_format(ab, " code=0x%x", code);
  2203. audit_log_end(ab);
  2204. }
  2205. struct list_head *audit_killed_trees(void)
  2206. {
  2207. struct audit_context *ctx = current->audit_context;
  2208. if (likely(!ctx || !ctx->in_syscall))
  2209. return NULL;
  2210. return &ctx->killed_trees;
  2211. }