auditsc.c 67 KB

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