auditsc.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505
  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. static int audit_in_mask(const struct audit_krule *rule, unsigned long val)
  688. {
  689. int word, bit;
  690. if (val > 0xffffffff)
  691. return false;
  692. word = AUDIT_WORD(val);
  693. if (word >= AUDIT_BITMASK_SIZE)
  694. return false;
  695. bit = AUDIT_BIT(val);
  696. return rule->mask[word] & bit;
  697. }
  698. /* At syscall entry and exit time, this filter is called if the
  699. * audit_state is not low enough that auditing cannot take place, but is
  700. * also not high enough that we already know we have to write an audit
  701. * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
  702. */
  703. static enum audit_state audit_filter_syscall(struct task_struct *tsk,
  704. struct audit_context *ctx,
  705. struct list_head *list)
  706. {
  707. struct audit_entry *e;
  708. enum audit_state state;
  709. if (audit_pid && tsk->tgid == audit_pid)
  710. return AUDIT_DISABLED;
  711. rcu_read_lock();
  712. if (!list_empty(list)) {
  713. list_for_each_entry_rcu(e, list, list) {
  714. if (audit_in_mask(&e->rule, ctx->major) &&
  715. audit_filter_rules(tsk, &e->rule, ctx, NULL,
  716. &state, false)) {
  717. rcu_read_unlock();
  718. ctx->current_state = state;
  719. return state;
  720. }
  721. }
  722. }
  723. rcu_read_unlock();
  724. return AUDIT_BUILD_CONTEXT;
  725. }
  726. /*
  727. * Given an audit_name check the inode hash table to see if they match.
  728. * Called holding the rcu read lock to protect the use of audit_inode_hash
  729. */
  730. static int audit_filter_inode_name(struct task_struct *tsk,
  731. struct audit_names *n,
  732. struct audit_context *ctx) {
  733. int h = audit_hash_ino((u32)n->ino);
  734. struct list_head *list = &audit_inode_hash[h];
  735. struct audit_entry *e;
  736. enum audit_state state;
  737. if (list_empty(list))
  738. return 0;
  739. list_for_each_entry_rcu(e, list, list) {
  740. if (audit_in_mask(&e->rule, ctx->major) &&
  741. audit_filter_rules(tsk, &e->rule, ctx, n, &state, false)) {
  742. ctx->current_state = state;
  743. return 1;
  744. }
  745. }
  746. return 0;
  747. }
  748. /* At syscall exit time, this filter is called if any audit_names have been
  749. * collected during syscall processing. We only check rules in sublists at hash
  750. * buckets applicable to the inode numbers in audit_names.
  751. * Regarding audit_state, same rules apply as for audit_filter_syscall().
  752. */
  753. void audit_filter_inodes(struct task_struct *tsk, struct audit_context *ctx)
  754. {
  755. struct audit_names *n;
  756. if (audit_pid && tsk->tgid == audit_pid)
  757. return;
  758. rcu_read_lock();
  759. list_for_each_entry(n, &ctx->names_list, list) {
  760. if (audit_filter_inode_name(tsk, n, ctx))
  761. break;
  762. }
  763. rcu_read_unlock();
  764. }
  765. /* Transfer the audit context pointer to the caller, clearing it in the tsk's struct */
  766. static inline struct audit_context *audit_take_context(struct task_struct *tsk,
  767. int return_valid,
  768. long return_code)
  769. {
  770. struct audit_context *context = tsk->audit_context;
  771. if (!context)
  772. return NULL;
  773. context->return_valid = return_valid;
  774. /*
  775. * we need to fix up the return code in the audit logs if the actual
  776. * return codes are later going to be fixed up by the arch specific
  777. * signal handlers
  778. *
  779. * This is actually a test for:
  780. * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
  781. * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
  782. *
  783. * but is faster than a bunch of ||
  784. */
  785. if (unlikely(return_code <= -ERESTARTSYS) &&
  786. (return_code >= -ERESTART_RESTARTBLOCK) &&
  787. (return_code != -ENOIOCTLCMD))
  788. context->return_code = -EINTR;
  789. else
  790. context->return_code = return_code;
  791. if (context->in_syscall && !context->dummy) {
  792. audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
  793. audit_filter_inodes(tsk, context);
  794. }
  795. tsk->audit_context = NULL;
  796. return context;
  797. }
  798. static inline void audit_proctitle_free(struct audit_context *context)
  799. {
  800. kfree(context->proctitle.value);
  801. context->proctitle.value = NULL;
  802. context->proctitle.len = 0;
  803. }
  804. static inline void audit_free_names(struct audit_context *context)
  805. {
  806. struct audit_names *n, *next;
  807. #if AUDIT_DEBUG == 2
  808. if (context->put_count + context->ino_count != context->name_count) {
  809. int i = 0;
  810. pr_err("%s:%d(:%d): major=%d in_syscall=%d"
  811. " name_count=%d put_count=%d ino_count=%d"
  812. " [NOT freeing]\n", __FILE__, __LINE__,
  813. context->serial, context->major, context->in_syscall,
  814. context->name_count, context->put_count,
  815. context->ino_count);
  816. list_for_each_entry(n, &context->names_list, list) {
  817. pr_err("names[%d] = %p = %s\n", i++, n->name,
  818. n->name->name ?: "(null)");
  819. }
  820. dump_stack();
  821. return;
  822. }
  823. #endif
  824. #if AUDIT_DEBUG
  825. context->put_count = 0;
  826. context->ino_count = 0;
  827. #endif
  828. list_for_each_entry_safe(n, next, &context->names_list, list) {
  829. list_del(&n->list);
  830. if (n->name && n->name_put)
  831. final_putname(n->name);
  832. if (n->should_free)
  833. kfree(n);
  834. }
  835. context->name_count = 0;
  836. path_put(&context->pwd);
  837. context->pwd.dentry = NULL;
  838. context->pwd.mnt = NULL;
  839. }
  840. static inline void audit_free_aux(struct audit_context *context)
  841. {
  842. struct audit_aux_data *aux;
  843. while ((aux = context->aux)) {
  844. context->aux = aux->next;
  845. kfree(aux);
  846. }
  847. while ((aux = context->aux_pids)) {
  848. context->aux_pids = aux->next;
  849. kfree(aux);
  850. }
  851. }
  852. static inline struct audit_context *audit_alloc_context(enum audit_state state)
  853. {
  854. struct audit_context *context;
  855. context = kzalloc(sizeof(*context), GFP_KERNEL);
  856. if (!context)
  857. return NULL;
  858. context->state = state;
  859. context->prio = state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
  860. INIT_LIST_HEAD(&context->killed_trees);
  861. INIT_LIST_HEAD(&context->names_list);
  862. return context;
  863. }
  864. /**
  865. * audit_alloc - allocate an audit context block for a task
  866. * @tsk: task
  867. *
  868. * Filter on the task information and allocate a per-task audit context
  869. * if necessary. Doing so turns on system call auditing for the
  870. * specified task. This is called from copy_process, so no lock is
  871. * needed.
  872. */
  873. int audit_alloc(struct task_struct *tsk)
  874. {
  875. struct audit_context *context;
  876. enum audit_state state;
  877. char *key = NULL;
  878. if (likely(!audit_ever_enabled))
  879. return 0; /* Return if not auditing. */
  880. state = audit_filter_task(tsk, &key);
  881. if (state == AUDIT_DISABLED) {
  882. clear_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
  883. return 0;
  884. }
  885. if (!(context = audit_alloc_context(state))) {
  886. kfree(key);
  887. audit_log_lost("out of memory in audit_alloc");
  888. return -ENOMEM;
  889. }
  890. context->filterkey = key;
  891. tsk->audit_context = context;
  892. set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
  893. return 0;
  894. }
  895. static inline void audit_free_context(struct audit_context *context)
  896. {
  897. audit_free_names(context);
  898. unroll_tree_refs(context, NULL, 0);
  899. free_tree_refs(context);
  900. audit_free_aux(context);
  901. kfree(context->filterkey);
  902. kfree(context->sockaddr);
  903. audit_proctitle_free(context);
  904. kfree(context);
  905. }
  906. static int audit_log_pid_context(struct audit_context *context, pid_t pid,
  907. kuid_t auid, kuid_t uid, unsigned int sessionid,
  908. u32 sid, char *comm)
  909. {
  910. struct audit_buffer *ab;
  911. char *ctx = NULL;
  912. u32 len;
  913. int rc = 0;
  914. ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
  915. if (!ab)
  916. return rc;
  917. audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid,
  918. from_kuid(&init_user_ns, auid),
  919. from_kuid(&init_user_ns, uid), sessionid);
  920. if (sid) {
  921. if (security_secid_to_secctx(sid, &ctx, &len)) {
  922. audit_log_format(ab, " obj=(none)");
  923. rc = 1;
  924. } else {
  925. audit_log_format(ab, " obj=%s", ctx);
  926. security_release_secctx(ctx, len);
  927. }
  928. }
  929. audit_log_format(ab, " ocomm=");
  930. audit_log_untrustedstring(ab, comm);
  931. audit_log_end(ab);
  932. return rc;
  933. }
  934. /*
  935. * to_send and len_sent accounting are very loose estimates. We aren't
  936. * really worried about a hard cap to MAX_EXECVE_AUDIT_LEN so much as being
  937. * within about 500 bytes (next page boundary)
  938. *
  939. * why snprintf? an int is up to 12 digits long. if we just assumed when
  940. * logging that a[%d]= was going to be 16 characters long we would be wasting
  941. * space in every audit message. In one 7500 byte message we can log up to
  942. * about 1000 min size arguments. That comes down to about 50% waste of space
  943. * if we didn't do the snprintf to find out how long arg_num_len was.
  944. */
  945. static int audit_log_single_execve_arg(struct audit_context *context,
  946. struct audit_buffer **ab,
  947. int arg_num,
  948. size_t *len_sent,
  949. const char __user *p,
  950. char *buf)
  951. {
  952. char arg_num_len_buf[12];
  953. const char __user *tmp_p = p;
  954. /* how many digits are in arg_num? 5 is the length of ' a=""' */
  955. size_t arg_num_len = snprintf(arg_num_len_buf, 12, "%d", arg_num) + 5;
  956. size_t len, len_left, to_send;
  957. size_t max_execve_audit_len = MAX_EXECVE_AUDIT_LEN;
  958. unsigned int i, has_cntl = 0, too_long = 0;
  959. int ret;
  960. /* strnlen_user includes the null we don't want to send */
  961. len_left = len = strnlen_user(p, MAX_ARG_STRLEN) - 1;
  962. /*
  963. * We just created this mm, if we can't find the strings
  964. * we just copied into it something is _very_ wrong. Similar
  965. * for strings that are too long, we should not have created
  966. * any.
  967. */
  968. if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
  969. WARN_ON(1);
  970. send_sig(SIGKILL, current, 0);
  971. return -1;
  972. }
  973. /* walk the whole argument looking for non-ascii chars */
  974. do {
  975. if (len_left > MAX_EXECVE_AUDIT_LEN)
  976. to_send = MAX_EXECVE_AUDIT_LEN;
  977. else
  978. to_send = len_left;
  979. ret = copy_from_user(buf, tmp_p, to_send);
  980. /*
  981. * There is no reason for this copy to be short. We just
  982. * copied them here, and the mm hasn't been exposed to user-
  983. * space yet.
  984. */
  985. if (ret) {
  986. WARN_ON(1);
  987. send_sig(SIGKILL, current, 0);
  988. return -1;
  989. }
  990. buf[to_send] = '\0';
  991. has_cntl = audit_string_contains_control(buf, to_send);
  992. if (has_cntl) {
  993. /*
  994. * hex messages get logged as 2 bytes, so we can only
  995. * send half as much in each message
  996. */
  997. max_execve_audit_len = MAX_EXECVE_AUDIT_LEN / 2;
  998. break;
  999. }
  1000. len_left -= to_send;
  1001. tmp_p += to_send;
  1002. } while (len_left > 0);
  1003. len_left = len;
  1004. if (len > max_execve_audit_len)
  1005. too_long = 1;
  1006. /* rewalk the argument actually logging the message */
  1007. for (i = 0; len_left > 0; i++) {
  1008. int room_left;
  1009. if (len_left > max_execve_audit_len)
  1010. to_send = max_execve_audit_len;
  1011. else
  1012. to_send = len_left;
  1013. /* do we have space left to send this argument in this ab? */
  1014. room_left = MAX_EXECVE_AUDIT_LEN - arg_num_len - *len_sent;
  1015. if (has_cntl)
  1016. room_left -= (to_send * 2);
  1017. else
  1018. room_left -= to_send;
  1019. if (room_left < 0) {
  1020. *len_sent = 0;
  1021. audit_log_end(*ab);
  1022. *ab = audit_log_start(context, GFP_KERNEL, AUDIT_EXECVE);
  1023. if (!*ab)
  1024. return 0;
  1025. }
  1026. /*
  1027. * first record needs to say how long the original string was
  1028. * so we can be sure nothing was lost.
  1029. */
  1030. if ((i == 0) && (too_long))
  1031. audit_log_format(*ab, " a%d_len=%zu", arg_num,
  1032. has_cntl ? 2*len : len);
  1033. /*
  1034. * normally arguments are small enough to fit and we already
  1035. * filled buf above when we checked for control characters
  1036. * so don't bother with another copy_from_user
  1037. */
  1038. if (len >= max_execve_audit_len)
  1039. ret = copy_from_user(buf, p, to_send);
  1040. else
  1041. ret = 0;
  1042. if (ret) {
  1043. WARN_ON(1);
  1044. send_sig(SIGKILL, current, 0);
  1045. return -1;
  1046. }
  1047. buf[to_send] = '\0';
  1048. /* actually log it */
  1049. audit_log_format(*ab, " a%d", arg_num);
  1050. if (too_long)
  1051. audit_log_format(*ab, "[%d]", i);
  1052. audit_log_format(*ab, "=");
  1053. if (has_cntl)
  1054. audit_log_n_hex(*ab, buf, to_send);
  1055. else
  1056. audit_log_string(*ab, buf);
  1057. p += to_send;
  1058. len_left -= to_send;
  1059. *len_sent += arg_num_len;
  1060. if (has_cntl)
  1061. *len_sent += to_send * 2;
  1062. else
  1063. *len_sent += to_send;
  1064. }
  1065. /* include the null we didn't log */
  1066. return len + 1;
  1067. }
  1068. static void audit_log_execve_info(struct audit_context *context,
  1069. struct audit_buffer **ab)
  1070. {
  1071. int i, len;
  1072. size_t len_sent = 0;
  1073. const char __user *p;
  1074. char *buf;
  1075. p = (const char __user *)current->mm->arg_start;
  1076. audit_log_format(*ab, "argc=%d", context->execve.argc);
  1077. /*
  1078. * we need some kernel buffer to hold the userspace args. Just
  1079. * allocate one big one rather than allocating one of the right size
  1080. * for every single argument inside audit_log_single_execve_arg()
  1081. * should be <8k allocation so should be pretty safe.
  1082. */
  1083. buf = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
  1084. if (!buf) {
  1085. audit_panic("out of memory for argv string");
  1086. return;
  1087. }
  1088. for (i = 0; i < context->execve.argc; i++) {
  1089. len = audit_log_single_execve_arg(context, ab, i,
  1090. &len_sent, p, buf);
  1091. if (len <= 0)
  1092. break;
  1093. p += len;
  1094. }
  1095. kfree(buf);
  1096. }
  1097. static void show_special(struct audit_context *context, int *call_panic)
  1098. {
  1099. struct audit_buffer *ab;
  1100. int i;
  1101. ab = audit_log_start(context, GFP_KERNEL, context->type);
  1102. if (!ab)
  1103. return;
  1104. switch (context->type) {
  1105. case AUDIT_SOCKETCALL: {
  1106. int nargs = context->socketcall.nargs;
  1107. audit_log_format(ab, "nargs=%d", nargs);
  1108. for (i = 0; i < nargs; i++)
  1109. audit_log_format(ab, " a%d=%lx", i,
  1110. context->socketcall.args[i]);
  1111. break; }
  1112. case AUDIT_IPC: {
  1113. u32 osid = context->ipc.osid;
  1114. audit_log_format(ab, "ouid=%u ogid=%u mode=%#ho",
  1115. from_kuid(&init_user_ns, context->ipc.uid),
  1116. from_kgid(&init_user_ns, context->ipc.gid),
  1117. context->ipc.mode);
  1118. if (osid) {
  1119. char *ctx = NULL;
  1120. u32 len;
  1121. if (security_secid_to_secctx(osid, &ctx, &len)) {
  1122. audit_log_format(ab, " osid=%u", osid);
  1123. *call_panic = 1;
  1124. } else {
  1125. audit_log_format(ab, " obj=%s", ctx);
  1126. security_release_secctx(ctx, len);
  1127. }
  1128. }
  1129. if (context->ipc.has_perm) {
  1130. audit_log_end(ab);
  1131. ab = audit_log_start(context, GFP_KERNEL,
  1132. AUDIT_IPC_SET_PERM);
  1133. if (unlikely(!ab))
  1134. return;
  1135. audit_log_format(ab,
  1136. "qbytes=%lx ouid=%u ogid=%u mode=%#ho",
  1137. context->ipc.qbytes,
  1138. context->ipc.perm_uid,
  1139. context->ipc.perm_gid,
  1140. context->ipc.perm_mode);
  1141. }
  1142. break; }
  1143. case AUDIT_MQ_OPEN: {
  1144. audit_log_format(ab,
  1145. "oflag=0x%x mode=%#ho mq_flags=0x%lx mq_maxmsg=%ld "
  1146. "mq_msgsize=%ld mq_curmsgs=%ld",
  1147. context->mq_open.oflag, context->mq_open.mode,
  1148. context->mq_open.attr.mq_flags,
  1149. context->mq_open.attr.mq_maxmsg,
  1150. context->mq_open.attr.mq_msgsize,
  1151. context->mq_open.attr.mq_curmsgs);
  1152. break; }
  1153. case AUDIT_MQ_SENDRECV: {
  1154. audit_log_format(ab,
  1155. "mqdes=%d msg_len=%zd msg_prio=%u "
  1156. "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
  1157. context->mq_sendrecv.mqdes,
  1158. context->mq_sendrecv.msg_len,
  1159. context->mq_sendrecv.msg_prio,
  1160. context->mq_sendrecv.abs_timeout.tv_sec,
  1161. context->mq_sendrecv.abs_timeout.tv_nsec);
  1162. break; }
  1163. case AUDIT_MQ_NOTIFY: {
  1164. audit_log_format(ab, "mqdes=%d sigev_signo=%d",
  1165. context->mq_notify.mqdes,
  1166. context->mq_notify.sigev_signo);
  1167. break; }
  1168. case AUDIT_MQ_GETSETATTR: {
  1169. struct mq_attr *attr = &context->mq_getsetattr.mqstat;
  1170. audit_log_format(ab,
  1171. "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
  1172. "mq_curmsgs=%ld ",
  1173. context->mq_getsetattr.mqdes,
  1174. attr->mq_flags, attr->mq_maxmsg,
  1175. attr->mq_msgsize, attr->mq_curmsgs);
  1176. break; }
  1177. case AUDIT_CAPSET: {
  1178. audit_log_format(ab, "pid=%d", context->capset.pid);
  1179. audit_log_cap(ab, "cap_pi", &context->capset.cap.inheritable);
  1180. audit_log_cap(ab, "cap_pp", &context->capset.cap.permitted);
  1181. audit_log_cap(ab, "cap_pe", &context->capset.cap.effective);
  1182. break; }
  1183. case AUDIT_MMAP: {
  1184. audit_log_format(ab, "fd=%d flags=0x%x", context->mmap.fd,
  1185. context->mmap.flags);
  1186. break; }
  1187. case AUDIT_EXECVE: {
  1188. audit_log_execve_info(context, &ab);
  1189. break; }
  1190. }
  1191. audit_log_end(ab);
  1192. }
  1193. static inline int audit_proctitle_rtrim(char *proctitle, int len)
  1194. {
  1195. char *end = proctitle + len - 1;
  1196. while (end > proctitle && !isprint(*end))
  1197. end--;
  1198. /* catch the case where proctitle is only 1 non-print character */
  1199. len = end - proctitle + 1;
  1200. len -= isprint(proctitle[len-1]) == 0;
  1201. return len;
  1202. }
  1203. static void audit_log_proctitle(struct task_struct *tsk,
  1204. struct audit_context *context)
  1205. {
  1206. int res;
  1207. char *buf;
  1208. char *msg = "(null)";
  1209. int len = strlen(msg);
  1210. struct audit_buffer *ab;
  1211. ab = audit_log_start(context, GFP_KERNEL, AUDIT_PROCTITLE);
  1212. if (!ab)
  1213. return; /* audit_panic or being filtered */
  1214. audit_log_format(ab, "proctitle=");
  1215. /* Not cached */
  1216. if (!context->proctitle.value) {
  1217. buf = kmalloc(MAX_PROCTITLE_AUDIT_LEN, GFP_KERNEL);
  1218. if (!buf)
  1219. goto out;
  1220. /* Historically called this from procfs naming */
  1221. res = get_cmdline(tsk, buf, MAX_PROCTITLE_AUDIT_LEN);
  1222. if (res == 0) {
  1223. kfree(buf);
  1224. goto out;
  1225. }
  1226. res = audit_proctitle_rtrim(buf, res);
  1227. if (res == 0) {
  1228. kfree(buf);
  1229. goto out;
  1230. }
  1231. context->proctitle.value = buf;
  1232. context->proctitle.len = res;
  1233. }
  1234. msg = context->proctitle.value;
  1235. len = context->proctitle.len;
  1236. out:
  1237. audit_log_n_untrustedstring(ab, msg, len);
  1238. audit_log_end(ab);
  1239. }
  1240. static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
  1241. {
  1242. int i, call_panic = 0;
  1243. struct audit_buffer *ab;
  1244. struct audit_aux_data *aux;
  1245. struct audit_names *n;
  1246. /* tsk == current */
  1247. context->personality = tsk->personality;
  1248. ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
  1249. if (!ab)
  1250. return; /* audit_panic has been called */
  1251. audit_log_format(ab, "arch=%x syscall=%d",
  1252. context->arch, context->major);
  1253. if (context->personality != PER_LINUX)
  1254. audit_log_format(ab, " per=%lx", context->personality);
  1255. if (context->return_valid)
  1256. audit_log_format(ab, " success=%s exit=%ld",
  1257. (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
  1258. context->return_code);
  1259. audit_log_format(ab,
  1260. " a0=%lx a1=%lx a2=%lx a3=%lx items=%d",
  1261. context->argv[0],
  1262. context->argv[1],
  1263. context->argv[2],
  1264. context->argv[3],
  1265. context->name_count);
  1266. audit_log_task_info(ab, tsk);
  1267. audit_log_key(ab, context->filterkey);
  1268. audit_log_end(ab);
  1269. for (aux = context->aux; aux; aux = aux->next) {
  1270. ab = audit_log_start(context, GFP_KERNEL, aux->type);
  1271. if (!ab)
  1272. continue; /* audit_panic has been called */
  1273. switch (aux->type) {
  1274. case AUDIT_BPRM_FCAPS: {
  1275. struct audit_aux_data_bprm_fcaps *axs = (void *)aux;
  1276. audit_log_format(ab, "fver=%x", axs->fcap_ver);
  1277. audit_log_cap(ab, "fp", &axs->fcap.permitted);
  1278. audit_log_cap(ab, "fi", &axs->fcap.inheritable);
  1279. audit_log_format(ab, " fe=%d", axs->fcap.fE);
  1280. audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted);
  1281. audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable);
  1282. audit_log_cap(ab, "old_pe", &axs->old_pcap.effective);
  1283. audit_log_cap(ab, "new_pp", &axs->new_pcap.permitted);
  1284. audit_log_cap(ab, "new_pi", &axs->new_pcap.inheritable);
  1285. audit_log_cap(ab, "new_pe", &axs->new_pcap.effective);
  1286. break; }
  1287. }
  1288. audit_log_end(ab);
  1289. }
  1290. if (context->type)
  1291. show_special(context, &call_panic);
  1292. if (context->fds[0] >= 0) {
  1293. ab = audit_log_start(context, GFP_KERNEL, AUDIT_FD_PAIR);
  1294. if (ab) {
  1295. audit_log_format(ab, "fd0=%d fd1=%d",
  1296. context->fds[0], context->fds[1]);
  1297. audit_log_end(ab);
  1298. }
  1299. }
  1300. if (context->sockaddr_len) {
  1301. ab = audit_log_start(context, GFP_KERNEL, AUDIT_SOCKADDR);
  1302. if (ab) {
  1303. audit_log_format(ab, "saddr=");
  1304. audit_log_n_hex(ab, (void *)context->sockaddr,
  1305. context->sockaddr_len);
  1306. audit_log_end(ab);
  1307. }
  1308. }
  1309. for (aux = context->aux_pids; aux; aux = aux->next) {
  1310. struct audit_aux_data_pids *axs = (void *)aux;
  1311. for (i = 0; i < axs->pid_count; i++)
  1312. if (audit_log_pid_context(context, axs->target_pid[i],
  1313. axs->target_auid[i],
  1314. axs->target_uid[i],
  1315. axs->target_sessionid[i],
  1316. axs->target_sid[i],
  1317. axs->target_comm[i]))
  1318. call_panic = 1;
  1319. }
  1320. if (context->target_pid &&
  1321. audit_log_pid_context(context, context->target_pid,
  1322. context->target_auid, context->target_uid,
  1323. context->target_sessionid,
  1324. context->target_sid, context->target_comm))
  1325. call_panic = 1;
  1326. if (context->pwd.dentry && context->pwd.mnt) {
  1327. ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
  1328. if (ab) {
  1329. audit_log_d_path(ab, " cwd=", &context->pwd);
  1330. audit_log_end(ab);
  1331. }
  1332. }
  1333. i = 0;
  1334. list_for_each_entry(n, &context->names_list, list) {
  1335. if (n->hidden)
  1336. continue;
  1337. audit_log_name(context, n, NULL, i++, &call_panic);
  1338. }
  1339. audit_log_proctitle(tsk, context);
  1340. /* Send end of event record to help user space know we are finished */
  1341. ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
  1342. if (ab)
  1343. audit_log_end(ab);
  1344. if (call_panic)
  1345. audit_panic("error converting sid to string");
  1346. }
  1347. /**
  1348. * audit_free - free a per-task audit context
  1349. * @tsk: task whose audit context block to free
  1350. *
  1351. * Called from copy_process and do_exit
  1352. */
  1353. void __audit_free(struct task_struct *tsk)
  1354. {
  1355. struct audit_context *context;
  1356. context = audit_take_context(tsk, 0, 0);
  1357. if (!context)
  1358. return;
  1359. /* Check for system calls that do not go through the exit
  1360. * function (e.g., exit_group), then free context block.
  1361. * We use GFP_ATOMIC here because we might be doing this
  1362. * in the context of the idle thread */
  1363. /* that can happen only if we are called from do_exit() */
  1364. if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
  1365. audit_log_exit(context, tsk);
  1366. if (!list_empty(&context->killed_trees))
  1367. audit_kill_trees(&context->killed_trees);
  1368. audit_free_context(context);
  1369. }
  1370. /**
  1371. * audit_syscall_entry - fill in an audit record at syscall entry
  1372. * @arch: architecture type
  1373. * @major: major syscall type (function)
  1374. * @a1: additional syscall register 1
  1375. * @a2: additional syscall register 2
  1376. * @a3: additional syscall register 3
  1377. * @a4: additional syscall register 4
  1378. *
  1379. * Fill in audit context at syscall entry. This only happens if the
  1380. * audit context was created when the task was created and the state or
  1381. * filters demand the audit context be built. If the state from the
  1382. * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
  1383. * then the record will be written at syscall exit time (otherwise, it
  1384. * will only be written if another part of the kernel requests that it
  1385. * be written).
  1386. */
  1387. void __audit_syscall_entry(int arch, int major,
  1388. unsigned long a1, unsigned long a2,
  1389. unsigned long a3, unsigned long a4)
  1390. {
  1391. struct task_struct *tsk = current;
  1392. struct audit_context *context = tsk->audit_context;
  1393. enum audit_state state;
  1394. if (!context)
  1395. return;
  1396. BUG_ON(context->in_syscall || context->name_count);
  1397. if (!audit_enabled)
  1398. return;
  1399. context->arch = arch;
  1400. context->major = major;
  1401. context->argv[0] = a1;
  1402. context->argv[1] = a2;
  1403. context->argv[2] = a3;
  1404. context->argv[3] = a4;
  1405. state = context->state;
  1406. context->dummy = !audit_n_rules;
  1407. if (!context->dummy && state == AUDIT_BUILD_CONTEXT) {
  1408. context->prio = 0;
  1409. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
  1410. }
  1411. if (state == AUDIT_DISABLED)
  1412. return;
  1413. context->serial = 0;
  1414. context->ctime = CURRENT_TIME;
  1415. context->in_syscall = 1;
  1416. context->current_state = state;
  1417. context->ppid = 0;
  1418. }
  1419. /**
  1420. * audit_syscall_exit - deallocate audit context after a system call
  1421. * @success: success value of the syscall
  1422. * @return_code: return value of the syscall
  1423. *
  1424. * Tear down after system call. If the audit context has been marked as
  1425. * auditable (either because of the AUDIT_RECORD_CONTEXT state from
  1426. * filtering, or because some other part of the kernel wrote an audit
  1427. * message), then write out the syscall information. In call cases,
  1428. * free the names stored from getname().
  1429. */
  1430. void __audit_syscall_exit(int success, long return_code)
  1431. {
  1432. struct task_struct *tsk = current;
  1433. struct audit_context *context;
  1434. if (success)
  1435. success = AUDITSC_SUCCESS;
  1436. else
  1437. success = AUDITSC_FAILURE;
  1438. context = audit_take_context(tsk, success, return_code);
  1439. if (!context)
  1440. return;
  1441. if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
  1442. audit_log_exit(context, tsk);
  1443. context->in_syscall = 0;
  1444. context->prio = context->state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
  1445. if (!list_empty(&context->killed_trees))
  1446. audit_kill_trees(&context->killed_trees);
  1447. audit_free_names(context);
  1448. unroll_tree_refs(context, NULL, 0);
  1449. audit_free_aux(context);
  1450. context->aux = NULL;
  1451. context->aux_pids = NULL;
  1452. context->target_pid = 0;
  1453. context->target_sid = 0;
  1454. context->sockaddr_len = 0;
  1455. context->type = 0;
  1456. context->fds[0] = -1;
  1457. if (context->state != AUDIT_RECORD_CONTEXT) {
  1458. kfree(context->filterkey);
  1459. context->filterkey = NULL;
  1460. }
  1461. tsk->audit_context = context;
  1462. }
  1463. static inline void handle_one(const struct inode *inode)
  1464. {
  1465. #ifdef CONFIG_AUDIT_TREE
  1466. struct audit_context *context;
  1467. struct audit_tree_refs *p;
  1468. struct audit_chunk *chunk;
  1469. int count;
  1470. if (likely(hlist_empty(&inode->i_fsnotify_marks)))
  1471. return;
  1472. context = current->audit_context;
  1473. p = context->trees;
  1474. count = context->tree_count;
  1475. rcu_read_lock();
  1476. chunk = audit_tree_lookup(inode);
  1477. rcu_read_unlock();
  1478. if (!chunk)
  1479. return;
  1480. if (likely(put_tree_ref(context, chunk)))
  1481. return;
  1482. if (unlikely(!grow_tree_refs(context))) {
  1483. pr_warn("out of memory, audit has lost a tree reference\n");
  1484. audit_set_auditable(context);
  1485. audit_put_chunk(chunk);
  1486. unroll_tree_refs(context, p, count);
  1487. return;
  1488. }
  1489. put_tree_ref(context, chunk);
  1490. #endif
  1491. }
  1492. static void handle_path(const struct dentry *dentry)
  1493. {
  1494. #ifdef CONFIG_AUDIT_TREE
  1495. struct audit_context *context;
  1496. struct audit_tree_refs *p;
  1497. const struct dentry *d, *parent;
  1498. struct audit_chunk *drop;
  1499. unsigned long seq;
  1500. int count;
  1501. context = current->audit_context;
  1502. p = context->trees;
  1503. count = context->tree_count;
  1504. retry:
  1505. drop = NULL;
  1506. d = dentry;
  1507. rcu_read_lock();
  1508. seq = read_seqbegin(&rename_lock);
  1509. for(;;) {
  1510. struct inode *inode = d->d_inode;
  1511. if (inode && unlikely(!hlist_empty(&inode->i_fsnotify_marks))) {
  1512. struct audit_chunk *chunk;
  1513. chunk = audit_tree_lookup(inode);
  1514. if (chunk) {
  1515. if (unlikely(!put_tree_ref(context, chunk))) {
  1516. drop = chunk;
  1517. break;
  1518. }
  1519. }
  1520. }
  1521. parent = d->d_parent;
  1522. if (parent == d)
  1523. break;
  1524. d = parent;
  1525. }
  1526. if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
  1527. rcu_read_unlock();
  1528. if (!drop) {
  1529. /* just a race with rename */
  1530. unroll_tree_refs(context, p, count);
  1531. goto retry;
  1532. }
  1533. audit_put_chunk(drop);
  1534. if (grow_tree_refs(context)) {
  1535. /* OK, got more space */
  1536. unroll_tree_refs(context, p, count);
  1537. goto retry;
  1538. }
  1539. /* too bad */
  1540. pr_warn("out of memory, audit has lost a tree reference\n");
  1541. unroll_tree_refs(context, p, count);
  1542. audit_set_auditable(context);
  1543. return;
  1544. }
  1545. rcu_read_unlock();
  1546. #endif
  1547. }
  1548. static struct audit_names *audit_alloc_name(struct audit_context *context,
  1549. unsigned char type)
  1550. {
  1551. struct audit_names *aname;
  1552. if (context->name_count < AUDIT_NAMES) {
  1553. aname = &context->preallocated_names[context->name_count];
  1554. memset(aname, 0, sizeof(*aname));
  1555. } else {
  1556. aname = kzalloc(sizeof(*aname), GFP_NOFS);
  1557. if (!aname)
  1558. return NULL;
  1559. aname->should_free = true;
  1560. }
  1561. aname->ino = (unsigned long)-1;
  1562. aname->type = type;
  1563. list_add_tail(&aname->list, &context->names_list);
  1564. context->name_count++;
  1565. #if AUDIT_DEBUG
  1566. context->ino_count++;
  1567. #endif
  1568. return aname;
  1569. }
  1570. /**
  1571. * audit_reusename - fill out filename with info from existing entry
  1572. * @uptr: userland ptr to pathname
  1573. *
  1574. * Search the audit_names list for the current audit context. If there is an
  1575. * existing entry with a matching "uptr" then return the filename
  1576. * associated with that audit_name. If not, return NULL.
  1577. */
  1578. struct filename *
  1579. __audit_reusename(const __user char *uptr)
  1580. {
  1581. struct audit_context *context = current->audit_context;
  1582. struct audit_names *n;
  1583. list_for_each_entry(n, &context->names_list, list) {
  1584. if (!n->name)
  1585. continue;
  1586. if (n->name->uptr == uptr)
  1587. return n->name;
  1588. }
  1589. return NULL;
  1590. }
  1591. /**
  1592. * audit_getname - add a name to the list
  1593. * @name: name to add
  1594. *
  1595. * Add a name to the list of audit names for this context.
  1596. * Called from fs/namei.c:getname().
  1597. */
  1598. void __audit_getname(struct filename *name)
  1599. {
  1600. struct audit_context *context = current->audit_context;
  1601. struct audit_names *n;
  1602. if (!context->in_syscall) {
  1603. #if AUDIT_DEBUG == 2
  1604. pr_err("%s:%d(:%d): ignoring getname(%p)\n",
  1605. __FILE__, __LINE__, context->serial, name);
  1606. dump_stack();
  1607. #endif
  1608. return;
  1609. }
  1610. #if AUDIT_DEBUG
  1611. /* The filename _must_ have a populated ->name */
  1612. BUG_ON(!name->name);
  1613. #endif
  1614. n = audit_alloc_name(context, AUDIT_TYPE_UNKNOWN);
  1615. if (!n)
  1616. return;
  1617. n->name = name;
  1618. n->name_len = AUDIT_NAME_FULL;
  1619. n->name_put = true;
  1620. name->aname = n;
  1621. if (!context->pwd.dentry)
  1622. get_fs_pwd(current->fs, &context->pwd);
  1623. }
  1624. /* audit_putname - intercept a putname request
  1625. * @name: name to intercept and delay for putname
  1626. *
  1627. * If we have stored the name from getname in the audit context,
  1628. * then we delay the putname until syscall exit.
  1629. * Called from include/linux/fs.h:putname().
  1630. */
  1631. void audit_putname(struct filename *name)
  1632. {
  1633. struct audit_context *context = current->audit_context;
  1634. BUG_ON(!context);
  1635. if (!name->aname || !context->in_syscall) {
  1636. #if AUDIT_DEBUG == 2
  1637. pr_err("%s:%d(:%d): final_putname(%p)\n",
  1638. __FILE__, __LINE__, context->serial, name);
  1639. if (context->name_count) {
  1640. struct audit_names *n;
  1641. int i = 0;
  1642. list_for_each_entry(n, &context->names_list, list)
  1643. pr_err("name[%d] = %p = %s\n", i++, n->name,
  1644. n->name->name ?: "(null)");
  1645. }
  1646. #endif
  1647. final_putname(name);
  1648. }
  1649. #if AUDIT_DEBUG
  1650. else {
  1651. ++context->put_count;
  1652. if (context->put_count > context->name_count) {
  1653. pr_err("%s:%d(:%d): major=%d in_syscall=%d putname(%p)"
  1654. " name_count=%d put_count=%d\n",
  1655. __FILE__, __LINE__,
  1656. context->serial, context->major,
  1657. context->in_syscall, name->name,
  1658. context->name_count, context->put_count);
  1659. dump_stack();
  1660. }
  1661. }
  1662. #endif
  1663. }
  1664. /**
  1665. * __audit_inode - store the inode and device from a lookup
  1666. * @name: name being audited
  1667. * @dentry: dentry being audited
  1668. * @flags: attributes for this particular entry
  1669. */
  1670. void __audit_inode(struct filename *name, const struct dentry *dentry,
  1671. unsigned int flags)
  1672. {
  1673. struct audit_context *context = current->audit_context;
  1674. const struct inode *inode = dentry->d_inode;
  1675. struct audit_names *n;
  1676. bool parent = flags & AUDIT_INODE_PARENT;
  1677. if (!context->in_syscall)
  1678. return;
  1679. if (!name)
  1680. goto out_alloc;
  1681. #if AUDIT_DEBUG
  1682. /* The struct filename _must_ have a populated ->name */
  1683. BUG_ON(!name->name);
  1684. #endif
  1685. /*
  1686. * If we have a pointer to an audit_names entry already, then we can
  1687. * just use it directly if the type is correct.
  1688. */
  1689. n = name->aname;
  1690. if (n) {
  1691. if (parent) {
  1692. if (n->type == AUDIT_TYPE_PARENT ||
  1693. n->type == AUDIT_TYPE_UNKNOWN)
  1694. goto out;
  1695. } else {
  1696. if (n->type != AUDIT_TYPE_PARENT)
  1697. goto out;
  1698. }
  1699. }
  1700. list_for_each_entry_reverse(n, &context->names_list, list) {
  1701. /* does the name pointer match? */
  1702. if (!n->name || n->name->name != name->name)
  1703. continue;
  1704. /* match the correct record type */
  1705. if (parent) {
  1706. if (n->type == AUDIT_TYPE_PARENT ||
  1707. n->type == AUDIT_TYPE_UNKNOWN)
  1708. goto out;
  1709. } else {
  1710. if (n->type != AUDIT_TYPE_PARENT)
  1711. goto out;
  1712. }
  1713. }
  1714. out_alloc:
  1715. /* unable to find the name from a previous getname(). Allocate a new
  1716. * anonymous entry.
  1717. */
  1718. n = audit_alloc_name(context, AUDIT_TYPE_NORMAL);
  1719. if (!n)
  1720. return;
  1721. out:
  1722. if (parent) {
  1723. n->name_len = n->name ? parent_len(n->name->name) : AUDIT_NAME_FULL;
  1724. n->type = AUDIT_TYPE_PARENT;
  1725. if (flags & AUDIT_INODE_HIDDEN)
  1726. n->hidden = true;
  1727. } else {
  1728. n->name_len = AUDIT_NAME_FULL;
  1729. n->type = AUDIT_TYPE_NORMAL;
  1730. }
  1731. handle_path(dentry);
  1732. audit_copy_inode(n, dentry, inode);
  1733. }
  1734. /**
  1735. * __audit_inode_child - collect inode info for created/removed objects
  1736. * @parent: inode of dentry parent
  1737. * @dentry: dentry being audited
  1738. * @type: AUDIT_TYPE_* value that we're looking for
  1739. *
  1740. * For syscalls that create or remove filesystem objects, audit_inode
  1741. * can only collect information for the filesystem object's parent.
  1742. * This call updates the audit context with the child's information.
  1743. * Syscalls that create a new filesystem object must be hooked after
  1744. * the object is created. Syscalls that remove a filesystem object
  1745. * must be hooked prior, in order to capture the target inode during
  1746. * unsuccessful attempts.
  1747. */
  1748. void __audit_inode_child(const struct inode *parent,
  1749. const struct dentry *dentry,
  1750. const unsigned char type)
  1751. {
  1752. struct audit_context *context = current->audit_context;
  1753. const struct inode *inode = dentry->d_inode;
  1754. const char *dname = dentry->d_name.name;
  1755. struct audit_names *n, *found_parent = NULL, *found_child = NULL;
  1756. if (!context->in_syscall)
  1757. return;
  1758. if (inode)
  1759. handle_one(inode);
  1760. /* look for a parent entry first */
  1761. list_for_each_entry(n, &context->names_list, list) {
  1762. if (!n->name || n->type != AUDIT_TYPE_PARENT)
  1763. continue;
  1764. if (n->ino == parent->i_ino &&
  1765. !audit_compare_dname_path(dname, n->name->name, n->name_len)) {
  1766. found_parent = n;
  1767. break;
  1768. }
  1769. }
  1770. /* is there a matching child entry? */
  1771. list_for_each_entry(n, &context->names_list, list) {
  1772. /* can only match entries that have a name */
  1773. if (!n->name || n->type != type)
  1774. continue;
  1775. /* if we found a parent, make sure this one is a child of it */
  1776. if (found_parent && (n->name != found_parent->name))
  1777. continue;
  1778. if (!strcmp(dname, n->name->name) ||
  1779. !audit_compare_dname_path(dname, n->name->name,
  1780. found_parent ?
  1781. found_parent->name_len :
  1782. AUDIT_NAME_FULL)) {
  1783. found_child = n;
  1784. break;
  1785. }
  1786. }
  1787. if (!found_parent) {
  1788. /* create a new, "anonymous" parent record */
  1789. n = audit_alloc_name(context, AUDIT_TYPE_PARENT);
  1790. if (!n)
  1791. return;
  1792. audit_copy_inode(n, NULL, parent);
  1793. }
  1794. if (!found_child) {
  1795. found_child = audit_alloc_name(context, type);
  1796. if (!found_child)
  1797. return;
  1798. /* Re-use the name belonging to the slot for a matching parent
  1799. * directory. All names for this context are relinquished in
  1800. * audit_free_names() */
  1801. if (found_parent) {
  1802. found_child->name = found_parent->name;
  1803. found_child->name_len = AUDIT_NAME_FULL;
  1804. /* don't call __putname() */
  1805. found_child->name_put = false;
  1806. }
  1807. }
  1808. if (inode)
  1809. audit_copy_inode(found_child, dentry, inode);
  1810. else
  1811. found_child->ino = (unsigned long)-1;
  1812. }
  1813. EXPORT_SYMBOL_GPL(__audit_inode_child);
  1814. /**
  1815. * auditsc_get_stamp - get local copies of audit_context values
  1816. * @ctx: audit_context for the task
  1817. * @t: timespec to store time recorded in the audit_context
  1818. * @serial: serial value that is recorded in the audit_context
  1819. *
  1820. * Also sets the context as auditable.
  1821. */
  1822. int auditsc_get_stamp(struct audit_context *ctx,
  1823. struct timespec *t, unsigned int *serial)
  1824. {
  1825. if (!ctx->in_syscall)
  1826. return 0;
  1827. if (!ctx->serial)
  1828. ctx->serial = audit_serial();
  1829. t->tv_sec = ctx->ctime.tv_sec;
  1830. t->tv_nsec = ctx->ctime.tv_nsec;
  1831. *serial = ctx->serial;
  1832. if (!ctx->prio) {
  1833. ctx->prio = 1;
  1834. ctx->current_state = AUDIT_RECORD_CONTEXT;
  1835. }
  1836. return 1;
  1837. }
  1838. /* global counter which is incremented every time something logs in */
  1839. static atomic_t session_id = ATOMIC_INIT(0);
  1840. static int audit_set_loginuid_perm(kuid_t loginuid)
  1841. {
  1842. /* if we are unset, we don't need privs */
  1843. if (!audit_loginuid_set(current))
  1844. return 0;
  1845. /* if AUDIT_FEATURE_LOGINUID_IMMUTABLE means never ever allow a change*/
  1846. if (is_audit_feature_set(AUDIT_FEATURE_LOGINUID_IMMUTABLE))
  1847. return -EPERM;
  1848. /* it is set, you need permission */
  1849. if (!capable(CAP_AUDIT_CONTROL))
  1850. return -EPERM;
  1851. /* reject if this is not an unset and we don't allow that */
  1852. if (is_audit_feature_set(AUDIT_FEATURE_ONLY_UNSET_LOGINUID) && uid_valid(loginuid))
  1853. return -EPERM;
  1854. return 0;
  1855. }
  1856. static void audit_log_set_loginuid(kuid_t koldloginuid, kuid_t kloginuid,
  1857. unsigned int oldsessionid, unsigned int sessionid,
  1858. int rc)
  1859. {
  1860. struct audit_buffer *ab;
  1861. uid_t uid, oldloginuid, loginuid;
  1862. if (!audit_enabled)
  1863. return;
  1864. uid = from_kuid(&init_user_ns, task_uid(current));
  1865. oldloginuid = from_kuid(&init_user_ns, koldloginuid);
  1866. loginuid = from_kuid(&init_user_ns, kloginuid),
  1867. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
  1868. if (!ab)
  1869. return;
  1870. audit_log_format(ab, "pid=%d uid=%u", task_pid_nr(current), uid);
  1871. audit_log_task_context(ab);
  1872. audit_log_format(ab, " old-auid=%u auid=%u old-ses=%u ses=%u res=%d",
  1873. oldloginuid, loginuid, oldsessionid, sessionid, !rc);
  1874. audit_log_end(ab);
  1875. }
  1876. /**
  1877. * audit_set_loginuid - set current task's audit_context loginuid
  1878. * @loginuid: loginuid value
  1879. *
  1880. * Returns 0.
  1881. *
  1882. * Called (set) from fs/proc/base.c::proc_loginuid_write().
  1883. */
  1884. int audit_set_loginuid(kuid_t loginuid)
  1885. {
  1886. struct task_struct *task = current;
  1887. unsigned int oldsessionid, sessionid = (unsigned int)-1;
  1888. kuid_t oldloginuid;
  1889. int rc;
  1890. oldloginuid = audit_get_loginuid(current);
  1891. oldsessionid = audit_get_sessionid(current);
  1892. rc = audit_set_loginuid_perm(loginuid);
  1893. if (rc)
  1894. goto out;
  1895. /* are we setting or clearing? */
  1896. if (uid_valid(loginuid))
  1897. sessionid = (unsigned int)atomic_inc_return(&session_id);
  1898. task->sessionid = sessionid;
  1899. task->loginuid = loginuid;
  1900. out:
  1901. audit_log_set_loginuid(oldloginuid, loginuid, oldsessionid, sessionid, rc);
  1902. return rc;
  1903. }
  1904. /**
  1905. * __audit_mq_open - record audit data for a POSIX MQ open
  1906. * @oflag: open flag
  1907. * @mode: mode bits
  1908. * @attr: queue attributes
  1909. *
  1910. */
  1911. void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
  1912. {
  1913. struct audit_context *context = current->audit_context;
  1914. if (attr)
  1915. memcpy(&context->mq_open.attr, attr, sizeof(struct mq_attr));
  1916. else
  1917. memset(&context->mq_open.attr, 0, sizeof(struct mq_attr));
  1918. context->mq_open.oflag = oflag;
  1919. context->mq_open.mode = mode;
  1920. context->type = AUDIT_MQ_OPEN;
  1921. }
  1922. /**
  1923. * __audit_mq_sendrecv - record audit data for a POSIX MQ timed send/receive
  1924. * @mqdes: MQ descriptor
  1925. * @msg_len: Message length
  1926. * @msg_prio: Message priority
  1927. * @abs_timeout: Message timeout in absolute time
  1928. *
  1929. */
  1930. void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
  1931. const struct timespec *abs_timeout)
  1932. {
  1933. struct audit_context *context = current->audit_context;
  1934. struct timespec *p = &context->mq_sendrecv.abs_timeout;
  1935. if (abs_timeout)
  1936. memcpy(p, abs_timeout, sizeof(struct timespec));
  1937. else
  1938. memset(p, 0, sizeof(struct timespec));
  1939. context->mq_sendrecv.mqdes = mqdes;
  1940. context->mq_sendrecv.msg_len = msg_len;
  1941. context->mq_sendrecv.msg_prio = msg_prio;
  1942. context->type = AUDIT_MQ_SENDRECV;
  1943. }
  1944. /**
  1945. * __audit_mq_notify - record audit data for a POSIX MQ notify
  1946. * @mqdes: MQ descriptor
  1947. * @notification: Notification event
  1948. *
  1949. */
  1950. void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
  1951. {
  1952. struct audit_context *context = current->audit_context;
  1953. if (notification)
  1954. context->mq_notify.sigev_signo = notification->sigev_signo;
  1955. else
  1956. context->mq_notify.sigev_signo = 0;
  1957. context->mq_notify.mqdes = mqdes;
  1958. context->type = AUDIT_MQ_NOTIFY;
  1959. }
  1960. /**
  1961. * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
  1962. * @mqdes: MQ descriptor
  1963. * @mqstat: MQ flags
  1964. *
  1965. */
  1966. void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
  1967. {
  1968. struct audit_context *context = current->audit_context;
  1969. context->mq_getsetattr.mqdes = mqdes;
  1970. context->mq_getsetattr.mqstat = *mqstat;
  1971. context->type = AUDIT_MQ_GETSETATTR;
  1972. }
  1973. /**
  1974. * audit_ipc_obj - record audit data for ipc object
  1975. * @ipcp: ipc permissions
  1976. *
  1977. */
  1978. void __audit_ipc_obj(struct kern_ipc_perm *ipcp)
  1979. {
  1980. struct audit_context *context = current->audit_context;
  1981. context->ipc.uid = ipcp->uid;
  1982. context->ipc.gid = ipcp->gid;
  1983. context->ipc.mode = ipcp->mode;
  1984. context->ipc.has_perm = 0;
  1985. security_ipc_getsecid(ipcp, &context->ipc.osid);
  1986. context->type = AUDIT_IPC;
  1987. }
  1988. /**
  1989. * audit_ipc_set_perm - record audit data for new ipc permissions
  1990. * @qbytes: msgq bytes
  1991. * @uid: msgq user id
  1992. * @gid: msgq group id
  1993. * @mode: msgq mode (permissions)
  1994. *
  1995. * Called only after audit_ipc_obj().
  1996. */
  1997. void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode)
  1998. {
  1999. struct audit_context *context = current->audit_context;
  2000. context->ipc.qbytes = qbytes;
  2001. context->ipc.perm_uid = uid;
  2002. context->ipc.perm_gid = gid;
  2003. context->ipc.perm_mode = mode;
  2004. context->ipc.has_perm = 1;
  2005. }
  2006. void __audit_bprm(struct linux_binprm *bprm)
  2007. {
  2008. struct audit_context *context = current->audit_context;
  2009. context->type = AUDIT_EXECVE;
  2010. context->execve.argc = bprm->argc;
  2011. }
  2012. /**
  2013. * audit_socketcall - record audit data for sys_socketcall
  2014. * @nargs: number of args, which should not be more than AUDITSC_ARGS.
  2015. * @args: args array
  2016. *
  2017. */
  2018. int __audit_socketcall(int nargs, unsigned long *args)
  2019. {
  2020. struct audit_context *context = current->audit_context;
  2021. if (nargs <= 0 || nargs > AUDITSC_ARGS || !args)
  2022. return -EINVAL;
  2023. context->type = AUDIT_SOCKETCALL;
  2024. context->socketcall.nargs = nargs;
  2025. memcpy(context->socketcall.args, args, nargs * sizeof(unsigned long));
  2026. return 0;
  2027. }
  2028. /**
  2029. * __audit_fd_pair - record audit data for pipe and socketpair
  2030. * @fd1: the first file descriptor
  2031. * @fd2: the second file descriptor
  2032. *
  2033. */
  2034. void __audit_fd_pair(int fd1, int fd2)
  2035. {
  2036. struct audit_context *context = current->audit_context;
  2037. context->fds[0] = fd1;
  2038. context->fds[1] = fd2;
  2039. }
  2040. /**
  2041. * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
  2042. * @len: data length in user space
  2043. * @a: data address in kernel space
  2044. *
  2045. * Returns 0 for success or NULL context or < 0 on error.
  2046. */
  2047. int __audit_sockaddr(int len, void *a)
  2048. {
  2049. struct audit_context *context = current->audit_context;
  2050. if (!context->sockaddr) {
  2051. void *p = kmalloc(sizeof(struct sockaddr_storage), GFP_KERNEL);
  2052. if (!p)
  2053. return -ENOMEM;
  2054. context->sockaddr = p;
  2055. }
  2056. context->sockaddr_len = len;
  2057. memcpy(context->sockaddr, a, len);
  2058. return 0;
  2059. }
  2060. void __audit_ptrace(struct task_struct *t)
  2061. {
  2062. struct audit_context *context = current->audit_context;
  2063. context->target_pid = task_pid_nr(t);
  2064. context->target_auid = audit_get_loginuid(t);
  2065. context->target_uid = task_uid(t);
  2066. context->target_sessionid = audit_get_sessionid(t);
  2067. security_task_getsecid(t, &context->target_sid);
  2068. memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
  2069. }
  2070. /**
  2071. * audit_signal_info - record signal info for shutting down audit subsystem
  2072. * @sig: signal value
  2073. * @t: task being signaled
  2074. *
  2075. * If the audit subsystem is being terminated, record the task (pid)
  2076. * and uid that is doing that.
  2077. */
  2078. int __audit_signal_info(int sig, struct task_struct *t)
  2079. {
  2080. struct audit_aux_data_pids *axp;
  2081. struct task_struct *tsk = current;
  2082. struct audit_context *ctx = tsk->audit_context;
  2083. kuid_t uid = current_uid(), t_uid = task_uid(t);
  2084. if (audit_pid && t->tgid == audit_pid) {
  2085. if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1 || sig == SIGUSR2) {
  2086. audit_sig_pid = task_pid_nr(tsk);
  2087. if (uid_valid(tsk->loginuid))
  2088. audit_sig_uid = tsk->loginuid;
  2089. else
  2090. audit_sig_uid = uid;
  2091. security_task_getsecid(tsk, &audit_sig_sid);
  2092. }
  2093. if (!audit_signals || audit_dummy_context())
  2094. return 0;
  2095. }
  2096. /* optimize the common case by putting first signal recipient directly
  2097. * in audit_context */
  2098. if (!ctx->target_pid) {
  2099. ctx->target_pid = task_tgid_nr(t);
  2100. ctx->target_auid = audit_get_loginuid(t);
  2101. ctx->target_uid = t_uid;
  2102. ctx->target_sessionid = audit_get_sessionid(t);
  2103. security_task_getsecid(t, &ctx->target_sid);
  2104. memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
  2105. return 0;
  2106. }
  2107. axp = (void *)ctx->aux_pids;
  2108. if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
  2109. axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
  2110. if (!axp)
  2111. return -ENOMEM;
  2112. axp->d.type = AUDIT_OBJ_PID;
  2113. axp->d.next = ctx->aux_pids;
  2114. ctx->aux_pids = (void *)axp;
  2115. }
  2116. BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
  2117. axp->target_pid[axp->pid_count] = task_tgid_nr(t);
  2118. axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
  2119. axp->target_uid[axp->pid_count] = t_uid;
  2120. axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
  2121. security_task_getsecid(t, &axp->target_sid[axp->pid_count]);
  2122. memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
  2123. axp->pid_count++;
  2124. return 0;
  2125. }
  2126. /**
  2127. * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps
  2128. * @bprm: pointer to the bprm being processed
  2129. * @new: the proposed new credentials
  2130. * @old: the old credentials
  2131. *
  2132. * Simply check if the proc already has the caps given by the file and if not
  2133. * store the priv escalation info for later auditing at the end of the syscall
  2134. *
  2135. * -Eric
  2136. */
  2137. int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
  2138. const struct cred *new, const struct cred *old)
  2139. {
  2140. struct audit_aux_data_bprm_fcaps *ax;
  2141. struct audit_context *context = current->audit_context;
  2142. struct cpu_vfs_cap_data vcaps;
  2143. struct dentry *dentry;
  2144. ax = kmalloc(sizeof(*ax), GFP_KERNEL);
  2145. if (!ax)
  2146. return -ENOMEM;
  2147. ax->d.type = AUDIT_BPRM_FCAPS;
  2148. ax->d.next = context->aux;
  2149. context->aux = (void *)ax;
  2150. dentry = dget(bprm->file->f_dentry);
  2151. get_vfs_caps_from_disk(dentry, &vcaps);
  2152. dput(dentry);
  2153. ax->fcap.permitted = vcaps.permitted;
  2154. ax->fcap.inheritable = vcaps.inheritable;
  2155. ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
  2156. ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
  2157. ax->old_pcap.permitted = old->cap_permitted;
  2158. ax->old_pcap.inheritable = old->cap_inheritable;
  2159. ax->old_pcap.effective = old->cap_effective;
  2160. ax->new_pcap.permitted = new->cap_permitted;
  2161. ax->new_pcap.inheritable = new->cap_inheritable;
  2162. ax->new_pcap.effective = new->cap_effective;
  2163. return 0;
  2164. }
  2165. /**
  2166. * __audit_log_capset - store information about the arguments to the capset syscall
  2167. * @new: the new credentials
  2168. * @old: the old (current) credentials
  2169. *
  2170. * Record the aguments userspace sent to sys_capset for later printing by the
  2171. * audit system if applicable
  2172. */
  2173. void __audit_log_capset(const struct cred *new, const struct cred *old)
  2174. {
  2175. struct audit_context *context = current->audit_context;
  2176. context->capset.pid = task_pid_nr(current);
  2177. context->capset.cap.effective = new->cap_effective;
  2178. context->capset.cap.inheritable = new->cap_effective;
  2179. context->capset.cap.permitted = new->cap_permitted;
  2180. context->type = AUDIT_CAPSET;
  2181. }
  2182. void __audit_mmap_fd(int fd, int flags)
  2183. {
  2184. struct audit_context *context = current->audit_context;
  2185. context->mmap.fd = fd;
  2186. context->mmap.flags = flags;
  2187. context->type = AUDIT_MMAP;
  2188. }
  2189. static void audit_log_task(struct audit_buffer *ab)
  2190. {
  2191. kuid_t auid, uid;
  2192. kgid_t gid;
  2193. unsigned int sessionid;
  2194. struct mm_struct *mm = current->mm;
  2195. auid = audit_get_loginuid(current);
  2196. sessionid = audit_get_sessionid(current);
  2197. current_uid_gid(&uid, &gid);
  2198. audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
  2199. from_kuid(&init_user_ns, auid),
  2200. from_kuid(&init_user_ns, uid),
  2201. from_kgid(&init_user_ns, gid),
  2202. sessionid);
  2203. audit_log_task_context(ab);
  2204. audit_log_format(ab, " pid=%d comm=", task_pid_nr(current));
  2205. audit_log_untrustedstring(ab, current->comm);
  2206. if (mm) {
  2207. down_read(&mm->mmap_sem);
  2208. if (mm->exe_file)
  2209. audit_log_d_path(ab, " exe=", &mm->exe_file->f_path);
  2210. up_read(&mm->mmap_sem);
  2211. } else
  2212. audit_log_format(ab, " exe=(null)");
  2213. }
  2214. /**
  2215. * audit_core_dumps - record information about processes that end abnormally
  2216. * @signr: signal value
  2217. *
  2218. * If a process ends with a core dump, something fishy is going on and we
  2219. * should record the event for investigation.
  2220. */
  2221. void audit_core_dumps(long signr)
  2222. {
  2223. struct audit_buffer *ab;
  2224. if (!audit_enabled)
  2225. return;
  2226. if (signr == SIGQUIT) /* don't care for those */
  2227. return;
  2228. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
  2229. if (unlikely(!ab))
  2230. return;
  2231. audit_log_task(ab);
  2232. audit_log_format(ab, " sig=%ld", signr);
  2233. audit_log_end(ab);
  2234. }
  2235. void __audit_seccomp(unsigned long syscall, long signr, int code)
  2236. {
  2237. struct audit_buffer *ab;
  2238. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_SECCOMP);
  2239. if (unlikely(!ab))
  2240. return;
  2241. audit_log_task(ab);
  2242. audit_log_format(ab, " sig=%ld", signr);
  2243. audit_log_format(ab, " syscall=%ld", syscall);
  2244. audit_log_format(ab, " compat=%d", is_compat_task());
  2245. audit_log_format(ab, " ip=0x%lx", KSTK_EIP(current));
  2246. audit_log_format(ab, " code=0x%x", code);
  2247. audit_log_end(ab);
  2248. }
  2249. struct list_head *audit_killed_trees(void)
  2250. {
  2251. struct audit_context *ctx = current->audit_context;
  2252. if (likely(!ctx || !ctx->in_syscall))
  2253. return NULL;
  2254. return &ctx->killed_trees;
  2255. }