auditsc.c 63 KB

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