auditsc.c 65 KB

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