auditsc.c 59 KB

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