auditsc.c 56 KB

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