auditsc.c 63 KB

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