auditsc.c 65 KB

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