auditsc.c 66 KB

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