auditsc.c 58 KB

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