auditsc.c 66 KB

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