auditsc.c 67 KB

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