auditsc.c 66 KB

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