auditsc.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013
  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 <asm/types.h>
  48. #include <linux/fs.h>
  49. #include <linux/namei.h>
  50. #include <linux/mm.h>
  51. #include <linux/module.h>
  52. #include <linux/mount.h>
  53. #include <linux/socket.h>
  54. #include <linux/mqueue.h>
  55. #include <linux/audit.h>
  56. #include <linux/personality.h>
  57. #include <linux/time.h>
  58. #include <linux/netlink.h>
  59. #include <linux/compiler.h>
  60. #include <asm/unistd.h>
  61. #include <linux/security.h>
  62. #include <linux/list.h>
  63. #include <linux/tty.h>
  64. #include <linux/selinux.h>
  65. #include <linux/binfmts.h>
  66. #include <linux/highmem.h>
  67. #include <linux/syscalls.h>
  68. #include "audit.h"
  69. extern struct list_head audit_filter_list[];
  70. /* No syscall auditing will take place unless audit_enabled != 0. */
  71. extern int audit_enabled;
  72. /* AUDIT_NAMES is the number of slots we reserve in the audit_context
  73. * for saving names from getname(). */
  74. #define AUDIT_NAMES 20
  75. /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
  76. * audit_context from being used for nameless inodes from
  77. * path_lookup. */
  78. #define AUDIT_NAMES_RESERVED 7
  79. /* Indicates that audit should log the full pathname. */
  80. #define AUDIT_NAME_FULL -1
  81. /* number of audit rules */
  82. int audit_n_rules;
  83. /* determines whether we collect data for signals sent */
  84. int audit_signals;
  85. /* When fs/namei.c:getname() is called, we store the pointer in name and
  86. * we don't let putname() free it (instead we free all of the saved
  87. * pointers at syscall exit time).
  88. *
  89. * Further, in fs/namei.c:path_lookup() we store the inode and device. */
  90. struct audit_names {
  91. const char *name;
  92. int name_len; /* number of name's characters to log */
  93. unsigned name_put; /* call __putname() for this name */
  94. unsigned long ino;
  95. dev_t dev;
  96. umode_t mode;
  97. uid_t uid;
  98. gid_t gid;
  99. dev_t rdev;
  100. u32 osid;
  101. };
  102. struct audit_aux_data {
  103. struct audit_aux_data *next;
  104. int type;
  105. };
  106. #define AUDIT_AUX_IPCPERM 0
  107. /* Number of target pids per aux struct. */
  108. #define AUDIT_AUX_PIDS 16
  109. struct audit_aux_data_mq_open {
  110. struct audit_aux_data d;
  111. int oflag;
  112. mode_t mode;
  113. struct mq_attr attr;
  114. };
  115. struct audit_aux_data_mq_sendrecv {
  116. struct audit_aux_data d;
  117. mqd_t mqdes;
  118. size_t msg_len;
  119. unsigned int msg_prio;
  120. struct timespec abs_timeout;
  121. };
  122. struct audit_aux_data_mq_notify {
  123. struct audit_aux_data d;
  124. mqd_t mqdes;
  125. struct sigevent notification;
  126. };
  127. struct audit_aux_data_mq_getsetattr {
  128. struct audit_aux_data d;
  129. mqd_t mqdes;
  130. struct mq_attr mqstat;
  131. };
  132. struct audit_aux_data_ipcctl {
  133. struct audit_aux_data d;
  134. struct ipc_perm p;
  135. unsigned long qbytes;
  136. uid_t uid;
  137. gid_t gid;
  138. mode_t mode;
  139. u32 osid;
  140. };
  141. struct audit_aux_data_execve {
  142. struct audit_aux_data d;
  143. int argc;
  144. int envc;
  145. char mem[0];
  146. };
  147. struct audit_aux_data_socketcall {
  148. struct audit_aux_data d;
  149. int nargs;
  150. unsigned long args[0];
  151. };
  152. struct audit_aux_data_sockaddr {
  153. struct audit_aux_data d;
  154. int len;
  155. char a[0];
  156. };
  157. struct audit_aux_data_fd_pair {
  158. struct audit_aux_data d;
  159. int fd[2];
  160. };
  161. struct audit_aux_data_path {
  162. struct audit_aux_data d;
  163. struct dentry *dentry;
  164. struct vfsmount *mnt;
  165. };
  166. struct audit_aux_data_pids {
  167. struct audit_aux_data d;
  168. pid_t target_pid[AUDIT_AUX_PIDS];
  169. u32 target_sid[AUDIT_AUX_PIDS];
  170. int pid_count;
  171. };
  172. /* The per-task audit context. */
  173. struct audit_context {
  174. int dummy; /* must be the first element */
  175. int in_syscall; /* 1 if task is in a syscall */
  176. enum audit_state state;
  177. unsigned int serial; /* serial number for record */
  178. struct timespec ctime; /* time of syscall entry */
  179. uid_t loginuid; /* login uid (identity) */
  180. int major; /* syscall number */
  181. unsigned long argv[4]; /* syscall arguments */
  182. int return_valid; /* return code is valid */
  183. long return_code;/* syscall return code */
  184. int auditable; /* 1 if record should be written */
  185. int name_count;
  186. struct audit_names names[AUDIT_NAMES];
  187. char * filterkey; /* key for rule that triggered record */
  188. struct dentry * pwd;
  189. struct vfsmount * pwdmnt;
  190. struct audit_context *previous; /* For nested syscalls */
  191. struct audit_aux_data *aux;
  192. struct audit_aux_data *aux_pids;
  193. /* Save things to print about task_struct */
  194. pid_t pid, ppid;
  195. uid_t uid, euid, suid, fsuid;
  196. gid_t gid, egid, sgid, fsgid;
  197. unsigned long personality;
  198. int arch;
  199. pid_t target_pid;
  200. u32 target_sid;
  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. /* Determine if any context name data matches a rule's watch data */
  253. /* Compare a task_struct with an audit_rule. Return 1 on match, 0
  254. * otherwise. */
  255. static int audit_filter_rules(struct task_struct *tsk,
  256. struct audit_krule *rule,
  257. struct audit_context *ctx,
  258. struct audit_names *name,
  259. enum audit_state *state)
  260. {
  261. int i, j, need_sid = 1;
  262. u32 sid;
  263. for (i = 0; i < rule->field_count; i++) {
  264. struct audit_field *f = &rule->fields[i];
  265. int result = 0;
  266. switch (f->type) {
  267. case AUDIT_PID:
  268. result = audit_comparator(tsk->pid, f->op, f->val);
  269. break;
  270. case AUDIT_PPID:
  271. if (ctx) {
  272. if (!ctx->ppid)
  273. ctx->ppid = sys_getppid();
  274. result = audit_comparator(ctx->ppid, f->op, f->val);
  275. }
  276. break;
  277. case AUDIT_UID:
  278. result = audit_comparator(tsk->uid, f->op, f->val);
  279. break;
  280. case AUDIT_EUID:
  281. result = audit_comparator(tsk->euid, f->op, f->val);
  282. break;
  283. case AUDIT_SUID:
  284. result = audit_comparator(tsk->suid, f->op, f->val);
  285. break;
  286. case AUDIT_FSUID:
  287. result = audit_comparator(tsk->fsuid, f->op, f->val);
  288. break;
  289. case AUDIT_GID:
  290. result = audit_comparator(tsk->gid, f->op, f->val);
  291. break;
  292. case AUDIT_EGID:
  293. result = audit_comparator(tsk->egid, f->op, f->val);
  294. break;
  295. case AUDIT_SGID:
  296. result = audit_comparator(tsk->sgid, f->op, f->val);
  297. break;
  298. case AUDIT_FSGID:
  299. result = audit_comparator(tsk->fsgid, f->op, f->val);
  300. break;
  301. case AUDIT_PERS:
  302. result = audit_comparator(tsk->personality, f->op, f->val);
  303. break;
  304. case AUDIT_ARCH:
  305. if (ctx)
  306. result = audit_comparator(ctx->arch, f->op, f->val);
  307. break;
  308. case AUDIT_EXIT:
  309. if (ctx && ctx->return_valid)
  310. result = audit_comparator(ctx->return_code, f->op, f->val);
  311. break;
  312. case AUDIT_SUCCESS:
  313. if (ctx && ctx->return_valid) {
  314. if (f->val)
  315. result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
  316. else
  317. result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
  318. }
  319. break;
  320. case AUDIT_DEVMAJOR:
  321. if (name)
  322. result = audit_comparator(MAJOR(name->dev),
  323. f->op, f->val);
  324. else if (ctx) {
  325. for (j = 0; j < ctx->name_count; j++) {
  326. if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
  327. ++result;
  328. break;
  329. }
  330. }
  331. }
  332. break;
  333. case AUDIT_DEVMINOR:
  334. if (name)
  335. result = audit_comparator(MINOR(name->dev),
  336. f->op, f->val);
  337. else if (ctx) {
  338. for (j = 0; j < ctx->name_count; j++) {
  339. if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
  340. ++result;
  341. break;
  342. }
  343. }
  344. }
  345. break;
  346. case AUDIT_INODE:
  347. if (name)
  348. result = (name->ino == f->val);
  349. else if (ctx) {
  350. for (j = 0; j < ctx->name_count; j++) {
  351. if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
  352. ++result;
  353. break;
  354. }
  355. }
  356. }
  357. break;
  358. case AUDIT_WATCH:
  359. if (name && rule->watch->ino != (unsigned long)-1)
  360. result = (name->dev == rule->watch->dev &&
  361. name->ino == rule->watch->ino);
  362. break;
  363. case AUDIT_LOGINUID:
  364. result = 0;
  365. if (ctx)
  366. result = audit_comparator(ctx->loginuid, f->op, f->val);
  367. break;
  368. case AUDIT_SUBJ_USER:
  369. case AUDIT_SUBJ_ROLE:
  370. case AUDIT_SUBJ_TYPE:
  371. case AUDIT_SUBJ_SEN:
  372. case AUDIT_SUBJ_CLR:
  373. /* NOTE: this may return negative values indicating
  374. a temporary error. We simply treat this as a
  375. match for now to avoid losing information that
  376. may be wanted. An error message will also be
  377. logged upon error */
  378. if (f->se_rule) {
  379. if (need_sid) {
  380. selinux_get_task_sid(tsk, &sid);
  381. need_sid = 0;
  382. }
  383. result = selinux_audit_rule_match(sid, f->type,
  384. f->op,
  385. f->se_rule,
  386. ctx);
  387. }
  388. break;
  389. case AUDIT_OBJ_USER:
  390. case AUDIT_OBJ_ROLE:
  391. case AUDIT_OBJ_TYPE:
  392. case AUDIT_OBJ_LEV_LOW:
  393. case AUDIT_OBJ_LEV_HIGH:
  394. /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
  395. also applies here */
  396. if (f->se_rule) {
  397. /* Find files that match */
  398. if (name) {
  399. result = selinux_audit_rule_match(
  400. name->osid, f->type, f->op,
  401. f->se_rule, ctx);
  402. } else if (ctx) {
  403. for (j = 0; j < ctx->name_count; j++) {
  404. if (selinux_audit_rule_match(
  405. ctx->names[j].osid,
  406. f->type, f->op,
  407. f->se_rule, ctx)) {
  408. ++result;
  409. break;
  410. }
  411. }
  412. }
  413. /* Find ipc objects that match */
  414. if (ctx) {
  415. struct audit_aux_data *aux;
  416. for (aux = ctx->aux; aux;
  417. aux = aux->next) {
  418. if (aux->type == AUDIT_IPC) {
  419. struct audit_aux_data_ipcctl *axi = (void *)aux;
  420. if (selinux_audit_rule_match(axi->osid, f->type, f->op, f->se_rule, ctx)) {
  421. ++result;
  422. break;
  423. }
  424. }
  425. }
  426. }
  427. }
  428. break;
  429. case AUDIT_ARG0:
  430. case AUDIT_ARG1:
  431. case AUDIT_ARG2:
  432. case AUDIT_ARG3:
  433. if (ctx)
  434. result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
  435. break;
  436. case AUDIT_FILTERKEY:
  437. /* ignore this field for filtering */
  438. result = 1;
  439. break;
  440. case AUDIT_PERM:
  441. result = audit_match_perm(ctx, f->val);
  442. break;
  443. }
  444. if (!result)
  445. return 0;
  446. }
  447. if (rule->filterkey)
  448. ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
  449. switch (rule->action) {
  450. case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
  451. case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
  452. }
  453. return 1;
  454. }
  455. /* At process creation time, we can determine if system-call auditing is
  456. * completely disabled for this task. Since we only have the task
  457. * structure at this point, we can only check uid and gid.
  458. */
  459. static enum audit_state audit_filter_task(struct task_struct *tsk)
  460. {
  461. struct audit_entry *e;
  462. enum audit_state state;
  463. rcu_read_lock();
  464. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
  465. if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
  466. rcu_read_unlock();
  467. return state;
  468. }
  469. }
  470. rcu_read_unlock();
  471. return AUDIT_BUILD_CONTEXT;
  472. }
  473. /* At syscall entry and exit time, this filter is called if the
  474. * audit_state is not low enough that auditing cannot take place, but is
  475. * also not high enough that we already know we have to write an audit
  476. * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
  477. */
  478. static enum audit_state audit_filter_syscall(struct task_struct *tsk,
  479. struct audit_context *ctx,
  480. struct list_head *list)
  481. {
  482. struct audit_entry *e;
  483. enum audit_state state;
  484. if (audit_pid && tsk->tgid == audit_pid)
  485. return AUDIT_DISABLED;
  486. rcu_read_lock();
  487. if (!list_empty(list)) {
  488. int word = AUDIT_WORD(ctx->major);
  489. int bit = AUDIT_BIT(ctx->major);
  490. list_for_each_entry_rcu(e, list, list) {
  491. if ((e->rule.mask[word] & bit) == bit &&
  492. audit_filter_rules(tsk, &e->rule, ctx, NULL,
  493. &state)) {
  494. rcu_read_unlock();
  495. return state;
  496. }
  497. }
  498. }
  499. rcu_read_unlock();
  500. return AUDIT_BUILD_CONTEXT;
  501. }
  502. /* At syscall exit time, this filter is called if any audit_names[] have been
  503. * collected during syscall processing. We only check rules in sublists at hash
  504. * buckets applicable to the inode numbers in audit_names[].
  505. * Regarding audit_state, same rules apply as for audit_filter_syscall().
  506. */
  507. enum audit_state audit_filter_inodes(struct task_struct *tsk,
  508. struct audit_context *ctx)
  509. {
  510. int i;
  511. struct audit_entry *e;
  512. enum audit_state state;
  513. if (audit_pid && tsk->tgid == audit_pid)
  514. return AUDIT_DISABLED;
  515. rcu_read_lock();
  516. for (i = 0; i < ctx->name_count; i++) {
  517. int word = AUDIT_WORD(ctx->major);
  518. int bit = AUDIT_BIT(ctx->major);
  519. struct audit_names *n = &ctx->names[i];
  520. int h = audit_hash_ino((u32)n->ino);
  521. struct list_head *list = &audit_inode_hash[h];
  522. if (list_empty(list))
  523. continue;
  524. list_for_each_entry_rcu(e, list, list) {
  525. if ((e->rule.mask[word] & bit) == bit &&
  526. audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
  527. rcu_read_unlock();
  528. return state;
  529. }
  530. }
  531. }
  532. rcu_read_unlock();
  533. return AUDIT_BUILD_CONTEXT;
  534. }
  535. void audit_set_auditable(struct audit_context *ctx)
  536. {
  537. ctx->auditable = 1;
  538. }
  539. static inline struct audit_context *audit_get_context(struct task_struct *tsk,
  540. int return_valid,
  541. int return_code)
  542. {
  543. struct audit_context *context = tsk->audit_context;
  544. if (likely(!context))
  545. return NULL;
  546. context->return_valid = return_valid;
  547. context->return_code = return_code;
  548. if (context->in_syscall && !context->dummy && !context->auditable) {
  549. enum audit_state state;
  550. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
  551. if (state == AUDIT_RECORD_CONTEXT) {
  552. context->auditable = 1;
  553. goto get_context;
  554. }
  555. state = audit_filter_inodes(tsk, context);
  556. if (state == AUDIT_RECORD_CONTEXT)
  557. context->auditable = 1;
  558. }
  559. get_context:
  560. tsk->audit_context = NULL;
  561. return context;
  562. }
  563. static inline void audit_free_names(struct audit_context *context)
  564. {
  565. int i;
  566. #if AUDIT_DEBUG == 2
  567. if (context->auditable
  568. ||context->put_count + context->ino_count != context->name_count) {
  569. printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
  570. " name_count=%d put_count=%d"
  571. " ino_count=%d [NOT freeing]\n",
  572. __FILE__, __LINE__,
  573. context->serial, context->major, context->in_syscall,
  574. context->name_count, context->put_count,
  575. context->ino_count);
  576. for (i = 0; i < context->name_count; i++) {
  577. printk(KERN_ERR "names[%d] = %p = %s\n", i,
  578. context->names[i].name,
  579. context->names[i].name ?: "(null)");
  580. }
  581. dump_stack();
  582. return;
  583. }
  584. #endif
  585. #if AUDIT_DEBUG
  586. context->put_count = 0;
  587. context->ino_count = 0;
  588. #endif
  589. for (i = 0; i < context->name_count; i++) {
  590. if (context->names[i].name && context->names[i].name_put)
  591. __putname(context->names[i].name);
  592. }
  593. context->name_count = 0;
  594. if (context->pwd)
  595. dput(context->pwd);
  596. if (context->pwdmnt)
  597. mntput(context->pwdmnt);
  598. context->pwd = NULL;
  599. context->pwdmnt = NULL;
  600. }
  601. static inline void audit_free_aux(struct audit_context *context)
  602. {
  603. struct audit_aux_data *aux;
  604. while ((aux = context->aux)) {
  605. if (aux->type == AUDIT_AVC_PATH) {
  606. struct audit_aux_data_path *axi = (void *)aux;
  607. dput(axi->dentry);
  608. mntput(axi->mnt);
  609. }
  610. context->aux = aux->next;
  611. kfree(aux);
  612. }
  613. while ((aux = context->aux_pids)) {
  614. context->aux_pids = aux->next;
  615. kfree(aux);
  616. }
  617. }
  618. static inline void audit_zero_context(struct audit_context *context,
  619. enum audit_state state)
  620. {
  621. uid_t loginuid = context->loginuid;
  622. memset(context, 0, sizeof(*context));
  623. context->state = state;
  624. context->loginuid = loginuid;
  625. }
  626. static inline struct audit_context *audit_alloc_context(enum audit_state state)
  627. {
  628. struct audit_context *context;
  629. if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
  630. return NULL;
  631. audit_zero_context(context, state);
  632. return context;
  633. }
  634. /**
  635. * audit_alloc - allocate an audit context block for a task
  636. * @tsk: task
  637. *
  638. * Filter on the task information and allocate a per-task audit context
  639. * if necessary. Doing so turns on system call auditing for the
  640. * specified task. This is called from copy_process, so no lock is
  641. * needed.
  642. */
  643. int audit_alloc(struct task_struct *tsk)
  644. {
  645. struct audit_context *context;
  646. enum audit_state state;
  647. if (likely(!audit_enabled))
  648. return 0; /* Return if not auditing. */
  649. state = audit_filter_task(tsk);
  650. if (likely(state == AUDIT_DISABLED))
  651. return 0;
  652. if (!(context = audit_alloc_context(state))) {
  653. audit_log_lost("out of memory in audit_alloc");
  654. return -ENOMEM;
  655. }
  656. /* Preserve login uid */
  657. context->loginuid = -1;
  658. if (current->audit_context)
  659. context->loginuid = current->audit_context->loginuid;
  660. tsk->audit_context = context;
  661. set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
  662. return 0;
  663. }
  664. static inline void audit_free_context(struct audit_context *context)
  665. {
  666. struct audit_context *previous;
  667. int count = 0;
  668. do {
  669. previous = context->previous;
  670. if (previous || (count && count < 10)) {
  671. ++count;
  672. printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
  673. " freeing multiple contexts (%d)\n",
  674. context->serial, context->major,
  675. context->name_count, count);
  676. }
  677. audit_free_names(context);
  678. audit_free_aux(context);
  679. kfree(context->filterkey);
  680. kfree(context);
  681. context = previous;
  682. } while (context);
  683. if (count >= 10)
  684. printk(KERN_ERR "audit: freed %d contexts\n", count);
  685. }
  686. void audit_log_task_context(struct audit_buffer *ab)
  687. {
  688. char *ctx = NULL;
  689. unsigned len;
  690. int error;
  691. u32 sid;
  692. selinux_get_task_sid(current, &sid);
  693. if (!sid)
  694. return;
  695. error = selinux_sid_to_string(sid, &ctx, &len);
  696. if (error) {
  697. if (error != -EINVAL)
  698. goto error_path;
  699. return;
  700. }
  701. audit_log_format(ab, " subj=%s", ctx);
  702. kfree(ctx);
  703. return;
  704. error_path:
  705. audit_panic("error in audit_log_task_context");
  706. return;
  707. }
  708. EXPORT_SYMBOL(audit_log_task_context);
  709. static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
  710. {
  711. char name[sizeof(tsk->comm)];
  712. struct mm_struct *mm = tsk->mm;
  713. struct vm_area_struct *vma;
  714. /* tsk == current */
  715. get_task_comm(name, tsk);
  716. audit_log_format(ab, " comm=");
  717. audit_log_untrustedstring(ab, name);
  718. if (mm) {
  719. down_read(&mm->mmap_sem);
  720. vma = mm->mmap;
  721. while (vma) {
  722. if ((vma->vm_flags & VM_EXECUTABLE) &&
  723. vma->vm_file) {
  724. audit_log_d_path(ab, "exe=",
  725. vma->vm_file->f_path.dentry,
  726. vma->vm_file->f_path.mnt);
  727. break;
  728. }
  729. vma = vma->vm_next;
  730. }
  731. up_read(&mm->mmap_sem);
  732. }
  733. audit_log_task_context(ab);
  734. }
  735. static int audit_log_pid_context(struct audit_context *context, pid_t pid,
  736. u32 sid)
  737. {
  738. struct audit_buffer *ab;
  739. char *s = NULL;
  740. u32 len;
  741. int rc = 0;
  742. ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
  743. if (!ab)
  744. return 1;
  745. if (selinux_sid_to_string(sid, &s, &len)) {
  746. audit_log_format(ab, "opid=%d obj=(none)", pid);
  747. rc = 1;
  748. } else
  749. audit_log_format(ab, "opid=%d obj=%s", pid, s);
  750. audit_log_end(ab);
  751. kfree(s);
  752. return rc;
  753. }
  754. static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
  755. {
  756. int i, call_panic = 0;
  757. struct audit_buffer *ab;
  758. struct audit_aux_data *aux;
  759. const char *tty;
  760. /* tsk == current */
  761. context->pid = tsk->pid;
  762. if (!context->ppid)
  763. context->ppid = sys_getppid();
  764. context->uid = tsk->uid;
  765. context->gid = tsk->gid;
  766. context->euid = tsk->euid;
  767. context->suid = tsk->suid;
  768. context->fsuid = tsk->fsuid;
  769. context->egid = tsk->egid;
  770. context->sgid = tsk->sgid;
  771. context->fsgid = tsk->fsgid;
  772. context->personality = tsk->personality;
  773. ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
  774. if (!ab)
  775. return; /* audit_panic has been called */
  776. audit_log_format(ab, "arch=%x syscall=%d",
  777. context->arch, context->major);
  778. if (context->personality != PER_LINUX)
  779. audit_log_format(ab, " per=%lx", context->personality);
  780. if (context->return_valid)
  781. audit_log_format(ab, " success=%s exit=%ld",
  782. (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
  783. context->return_code);
  784. mutex_lock(&tty_mutex);
  785. read_lock(&tasklist_lock);
  786. if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
  787. tty = tsk->signal->tty->name;
  788. else
  789. tty = "(none)";
  790. read_unlock(&tasklist_lock);
  791. audit_log_format(ab,
  792. " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
  793. " ppid=%d pid=%d auid=%u uid=%u gid=%u"
  794. " euid=%u suid=%u fsuid=%u"
  795. " egid=%u sgid=%u fsgid=%u tty=%s",
  796. context->argv[0],
  797. context->argv[1],
  798. context->argv[2],
  799. context->argv[3],
  800. context->name_count,
  801. context->ppid,
  802. context->pid,
  803. context->loginuid,
  804. context->uid,
  805. context->gid,
  806. context->euid, context->suid, context->fsuid,
  807. context->egid, context->sgid, context->fsgid, tty);
  808. mutex_unlock(&tty_mutex);
  809. audit_log_task_info(ab, tsk);
  810. if (context->filterkey) {
  811. audit_log_format(ab, " key=");
  812. audit_log_untrustedstring(ab, context->filterkey);
  813. } else
  814. audit_log_format(ab, " key=(null)");
  815. audit_log_end(ab);
  816. for (aux = context->aux; aux; aux = aux->next) {
  817. ab = audit_log_start(context, GFP_KERNEL, aux->type);
  818. if (!ab)
  819. continue; /* audit_panic has been called */
  820. switch (aux->type) {
  821. case AUDIT_MQ_OPEN: {
  822. struct audit_aux_data_mq_open *axi = (void *)aux;
  823. audit_log_format(ab,
  824. "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
  825. "mq_msgsize=%ld mq_curmsgs=%ld",
  826. axi->oflag, axi->mode, axi->attr.mq_flags,
  827. axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
  828. axi->attr.mq_curmsgs);
  829. break; }
  830. case AUDIT_MQ_SENDRECV: {
  831. struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
  832. audit_log_format(ab,
  833. "mqdes=%d msg_len=%zd msg_prio=%u "
  834. "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
  835. axi->mqdes, axi->msg_len, axi->msg_prio,
  836. axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
  837. break; }
  838. case AUDIT_MQ_NOTIFY: {
  839. struct audit_aux_data_mq_notify *axi = (void *)aux;
  840. audit_log_format(ab,
  841. "mqdes=%d sigev_signo=%d",
  842. axi->mqdes,
  843. axi->notification.sigev_signo);
  844. break; }
  845. case AUDIT_MQ_GETSETATTR: {
  846. struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
  847. audit_log_format(ab,
  848. "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
  849. "mq_curmsgs=%ld ",
  850. axi->mqdes,
  851. axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
  852. axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
  853. break; }
  854. case AUDIT_IPC: {
  855. struct audit_aux_data_ipcctl *axi = (void *)aux;
  856. audit_log_format(ab,
  857. "ouid=%u ogid=%u mode=%x",
  858. axi->uid, axi->gid, axi->mode);
  859. if (axi->osid != 0) {
  860. char *ctx = NULL;
  861. u32 len;
  862. if (selinux_sid_to_string(
  863. axi->osid, &ctx, &len)) {
  864. audit_log_format(ab, " osid=%u",
  865. axi->osid);
  866. call_panic = 1;
  867. } else
  868. audit_log_format(ab, " obj=%s", ctx);
  869. kfree(ctx);
  870. }
  871. break; }
  872. case AUDIT_IPC_SET_PERM: {
  873. struct audit_aux_data_ipcctl *axi = (void *)aux;
  874. audit_log_format(ab,
  875. "qbytes=%lx ouid=%u ogid=%u mode=%x",
  876. axi->qbytes, axi->uid, axi->gid, axi->mode);
  877. break; }
  878. case AUDIT_EXECVE: {
  879. struct audit_aux_data_execve *axi = (void *)aux;
  880. int i;
  881. const char *p;
  882. for (i = 0, p = axi->mem; i < axi->argc; i++) {
  883. audit_log_format(ab, "a%d=", i);
  884. p = audit_log_untrustedstring(ab, p);
  885. audit_log_format(ab, "\n");
  886. }
  887. break; }
  888. case AUDIT_SOCKETCALL: {
  889. int i;
  890. struct audit_aux_data_socketcall *axs = (void *)aux;
  891. audit_log_format(ab, "nargs=%d", axs->nargs);
  892. for (i=0; i<axs->nargs; i++)
  893. audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
  894. break; }
  895. case AUDIT_SOCKADDR: {
  896. struct audit_aux_data_sockaddr *axs = (void *)aux;
  897. audit_log_format(ab, "saddr=");
  898. audit_log_hex(ab, axs->a, axs->len);
  899. break; }
  900. case AUDIT_AVC_PATH: {
  901. struct audit_aux_data_path *axi = (void *)aux;
  902. audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
  903. break; }
  904. case AUDIT_FD_PAIR: {
  905. struct audit_aux_data_fd_pair *axs = (void *)aux;
  906. audit_log_format(ab, "fd0=%d fd1=%d", axs->fd[0], axs->fd[1]);
  907. break; }
  908. }
  909. audit_log_end(ab);
  910. }
  911. for (aux = context->aux_pids; aux; aux = aux->next) {
  912. struct audit_aux_data_pids *axs = (void *)aux;
  913. int i;
  914. for (i = 0; i < axs->pid_count; i++)
  915. if (audit_log_pid_context(context, axs->target_pid[i],
  916. axs->target_sid[i]))
  917. call_panic = 1;
  918. }
  919. if (context->target_pid &&
  920. audit_log_pid_context(context, context->target_pid,
  921. context->target_sid))
  922. call_panic = 1;
  923. if (context->pwd && context->pwdmnt) {
  924. ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
  925. if (ab) {
  926. audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
  927. audit_log_end(ab);
  928. }
  929. }
  930. for (i = 0; i < context->name_count; i++) {
  931. struct audit_names *n = &context->names[i];
  932. ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
  933. if (!ab)
  934. continue; /* audit_panic has been called */
  935. audit_log_format(ab, "item=%d", i);
  936. if (n->name) {
  937. switch(n->name_len) {
  938. case AUDIT_NAME_FULL:
  939. /* log the full path */
  940. audit_log_format(ab, " name=");
  941. audit_log_untrustedstring(ab, n->name);
  942. break;
  943. case 0:
  944. /* name was specified as a relative path and the
  945. * directory component is the cwd */
  946. audit_log_d_path(ab, " name=", context->pwd,
  947. context->pwdmnt);
  948. break;
  949. default:
  950. /* log the name's directory component */
  951. audit_log_format(ab, " name=");
  952. audit_log_n_untrustedstring(ab, n->name_len,
  953. n->name);
  954. }
  955. } else
  956. audit_log_format(ab, " name=(null)");
  957. if (n->ino != (unsigned long)-1) {
  958. audit_log_format(ab, " inode=%lu"
  959. " dev=%02x:%02x mode=%#o"
  960. " ouid=%u ogid=%u rdev=%02x:%02x",
  961. n->ino,
  962. MAJOR(n->dev),
  963. MINOR(n->dev),
  964. n->mode,
  965. n->uid,
  966. n->gid,
  967. MAJOR(n->rdev),
  968. MINOR(n->rdev));
  969. }
  970. if (n->osid != 0) {
  971. char *ctx = NULL;
  972. u32 len;
  973. if (selinux_sid_to_string(
  974. n->osid, &ctx, &len)) {
  975. audit_log_format(ab, " osid=%u", n->osid);
  976. call_panic = 2;
  977. } else
  978. audit_log_format(ab, " obj=%s", ctx);
  979. kfree(ctx);
  980. }
  981. audit_log_end(ab);
  982. }
  983. if (call_panic)
  984. audit_panic("error converting sid to string");
  985. }
  986. /**
  987. * audit_free - free a per-task audit context
  988. * @tsk: task whose audit context block to free
  989. *
  990. * Called from copy_process and do_exit
  991. */
  992. void audit_free(struct task_struct *tsk)
  993. {
  994. struct audit_context *context;
  995. context = audit_get_context(tsk, 0, 0);
  996. if (likely(!context))
  997. return;
  998. /* Check for system calls that do not go through the exit
  999. * function (e.g., exit_group), then free context block.
  1000. * We use GFP_ATOMIC here because we might be doing this
  1001. * in the context of the idle thread */
  1002. /* that can happen only if we are called from do_exit() */
  1003. if (context->in_syscall && context->auditable)
  1004. audit_log_exit(context, tsk);
  1005. audit_free_context(context);
  1006. }
  1007. /**
  1008. * audit_syscall_entry - fill in an audit record at syscall entry
  1009. * @tsk: task being audited
  1010. * @arch: architecture type
  1011. * @major: major syscall type (function)
  1012. * @a1: additional syscall register 1
  1013. * @a2: additional syscall register 2
  1014. * @a3: additional syscall register 3
  1015. * @a4: additional syscall register 4
  1016. *
  1017. * Fill in audit context at syscall entry. This only happens if the
  1018. * audit context was created when the task was created and the state or
  1019. * filters demand the audit context be built. If the state from the
  1020. * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
  1021. * then the record will be written at syscall exit time (otherwise, it
  1022. * will only be written if another part of the kernel requests that it
  1023. * be written).
  1024. */
  1025. void audit_syscall_entry(int arch, int major,
  1026. unsigned long a1, unsigned long a2,
  1027. unsigned long a3, unsigned long a4)
  1028. {
  1029. struct task_struct *tsk = current;
  1030. struct audit_context *context = tsk->audit_context;
  1031. enum audit_state state;
  1032. BUG_ON(!context);
  1033. /*
  1034. * This happens only on certain architectures that make system
  1035. * calls in kernel_thread via the entry.S interface, instead of
  1036. * with direct calls. (If you are porting to a new
  1037. * architecture, hitting this condition can indicate that you
  1038. * got the _exit/_leave calls backward in entry.S.)
  1039. *
  1040. * i386 no
  1041. * x86_64 no
  1042. * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
  1043. *
  1044. * This also happens with vm86 emulation in a non-nested manner
  1045. * (entries without exits), so this case must be caught.
  1046. */
  1047. if (context->in_syscall) {
  1048. struct audit_context *newctx;
  1049. #if AUDIT_DEBUG
  1050. printk(KERN_ERR
  1051. "audit(:%d) pid=%d in syscall=%d;"
  1052. " entering syscall=%d\n",
  1053. context->serial, tsk->pid, context->major, major);
  1054. #endif
  1055. newctx = audit_alloc_context(context->state);
  1056. if (newctx) {
  1057. newctx->previous = context;
  1058. context = newctx;
  1059. tsk->audit_context = newctx;
  1060. } else {
  1061. /* If we can't alloc a new context, the best we
  1062. * can do is to leak memory (any pending putname
  1063. * will be lost). The only other alternative is
  1064. * to abandon auditing. */
  1065. audit_zero_context(context, context->state);
  1066. }
  1067. }
  1068. BUG_ON(context->in_syscall || context->name_count);
  1069. if (!audit_enabled)
  1070. return;
  1071. context->arch = arch;
  1072. context->major = major;
  1073. context->argv[0] = a1;
  1074. context->argv[1] = a2;
  1075. context->argv[2] = a3;
  1076. context->argv[3] = a4;
  1077. state = context->state;
  1078. context->dummy = !audit_n_rules;
  1079. if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT))
  1080. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
  1081. if (likely(state == AUDIT_DISABLED))
  1082. return;
  1083. context->serial = 0;
  1084. context->ctime = CURRENT_TIME;
  1085. context->in_syscall = 1;
  1086. context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
  1087. context->ppid = 0;
  1088. }
  1089. /**
  1090. * audit_syscall_exit - deallocate audit context after a system call
  1091. * @tsk: task being audited
  1092. * @valid: success/failure flag
  1093. * @return_code: syscall return value
  1094. *
  1095. * Tear down after system call. If the audit context has been marked as
  1096. * auditable (either because of the AUDIT_RECORD_CONTEXT state from
  1097. * filtering, or because some other part of the kernel write an audit
  1098. * message), then write out the syscall information. In call cases,
  1099. * free the names stored from getname().
  1100. */
  1101. void audit_syscall_exit(int valid, long return_code)
  1102. {
  1103. struct task_struct *tsk = current;
  1104. struct audit_context *context;
  1105. context = audit_get_context(tsk, valid, return_code);
  1106. if (likely(!context))
  1107. return;
  1108. if (context->in_syscall && context->auditable)
  1109. audit_log_exit(context, tsk);
  1110. context->in_syscall = 0;
  1111. context->auditable = 0;
  1112. if (context->previous) {
  1113. struct audit_context *new_context = context->previous;
  1114. context->previous = NULL;
  1115. audit_free_context(context);
  1116. tsk->audit_context = new_context;
  1117. } else {
  1118. audit_free_names(context);
  1119. audit_free_aux(context);
  1120. context->aux = NULL;
  1121. context->aux_pids = NULL;
  1122. context->target_pid = 0;
  1123. context->target_sid = 0;
  1124. kfree(context->filterkey);
  1125. context->filterkey = NULL;
  1126. tsk->audit_context = context;
  1127. }
  1128. }
  1129. /**
  1130. * audit_getname - add a name to the list
  1131. * @name: name to add
  1132. *
  1133. * Add a name to the list of audit names for this context.
  1134. * Called from fs/namei.c:getname().
  1135. */
  1136. void __audit_getname(const char *name)
  1137. {
  1138. struct audit_context *context = current->audit_context;
  1139. if (IS_ERR(name) || !name)
  1140. return;
  1141. if (!context->in_syscall) {
  1142. #if AUDIT_DEBUG == 2
  1143. printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
  1144. __FILE__, __LINE__, context->serial, name);
  1145. dump_stack();
  1146. #endif
  1147. return;
  1148. }
  1149. BUG_ON(context->name_count >= AUDIT_NAMES);
  1150. context->names[context->name_count].name = name;
  1151. context->names[context->name_count].name_len = AUDIT_NAME_FULL;
  1152. context->names[context->name_count].name_put = 1;
  1153. context->names[context->name_count].ino = (unsigned long)-1;
  1154. context->names[context->name_count].osid = 0;
  1155. ++context->name_count;
  1156. if (!context->pwd) {
  1157. read_lock(&current->fs->lock);
  1158. context->pwd = dget(current->fs->pwd);
  1159. context->pwdmnt = mntget(current->fs->pwdmnt);
  1160. read_unlock(&current->fs->lock);
  1161. }
  1162. }
  1163. /* audit_putname - intercept a putname request
  1164. * @name: name to intercept and delay for putname
  1165. *
  1166. * If we have stored the name from getname in the audit context,
  1167. * then we delay the putname until syscall exit.
  1168. * Called from include/linux/fs.h:putname().
  1169. */
  1170. void audit_putname(const char *name)
  1171. {
  1172. struct audit_context *context = current->audit_context;
  1173. BUG_ON(!context);
  1174. if (!context->in_syscall) {
  1175. #if AUDIT_DEBUG == 2
  1176. printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
  1177. __FILE__, __LINE__, context->serial, name);
  1178. if (context->name_count) {
  1179. int i;
  1180. for (i = 0; i < context->name_count; i++)
  1181. printk(KERN_ERR "name[%d] = %p = %s\n", i,
  1182. context->names[i].name,
  1183. context->names[i].name ?: "(null)");
  1184. }
  1185. #endif
  1186. __putname(name);
  1187. }
  1188. #if AUDIT_DEBUG
  1189. else {
  1190. ++context->put_count;
  1191. if (context->put_count > context->name_count) {
  1192. printk(KERN_ERR "%s:%d(:%d): major=%d"
  1193. " in_syscall=%d putname(%p) name_count=%d"
  1194. " put_count=%d\n",
  1195. __FILE__, __LINE__,
  1196. context->serial, context->major,
  1197. context->in_syscall, name, context->name_count,
  1198. context->put_count);
  1199. dump_stack();
  1200. }
  1201. }
  1202. #endif
  1203. }
  1204. /* Copy inode data into an audit_names. */
  1205. static void audit_copy_inode(struct audit_names *name, const struct inode *inode)
  1206. {
  1207. name->ino = inode->i_ino;
  1208. name->dev = inode->i_sb->s_dev;
  1209. name->mode = inode->i_mode;
  1210. name->uid = inode->i_uid;
  1211. name->gid = inode->i_gid;
  1212. name->rdev = inode->i_rdev;
  1213. selinux_get_inode_sid(inode, &name->osid);
  1214. }
  1215. /**
  1216. * audit_inode - store the inode and device from a lookup
  1217. * @name: name being audited
  1218. * @inode: inode being audited
  1219. *
  1220. * Called from fs/namei.c:path_lookup().
  1221. */
  1222. void __audit_inode(const char *name, const struct inode *inode)
  1223. {
  1224. int idx;
  1225. struct audit_context *context = current->audit_context;
  1226. if (!context->in_syscall)
  1227. return;
  1228. if (context->name_count
  1229. && context->names[context->name_count-1].name
  1230. && context->names[context->name_count-1].name == name)
  1231. idx = context->name_count - 1;
  1232. else if (context->name_count > 1
  1233. && context->names[context->name_count-2].name
  1234. && context->names[context->name_count-2].name == name)
  1235. idx = context->name_count - 2;
  1236. else {
  1237. /* FIXME: how much do we care about inodes that have no
  1238. * associated name? */
  1239. if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
  1240. return;
  1241. idx = context->name_count++;
  1242. context->names[idx].name = NULL;
  1243. #if AUDIT_DEBUG
  1244. ++context->ino_count;
  1245. #endif
  1246. }
  1247. audit_copy_inode(&context->names[idx], inode);
  1248. }
  1249. /**
  1250. * audit_inode_child - collect inode info for created/removed objects
  1251. * @dname: inode's dentry name
  1252. * @inode: inode being audited
  1253. * @parent: inode of dentry parent
  1254. *
  1255. * For syscalls that create or remove filesystem objects, audit_inode
  1256. * can only collect information for the filesystem object's parent.
  1257. * This call updates the audit context with the child's information.
  1258. * Syscalls that create a new filesystem object must be hooked after
  1259. * the object is created. Syscalls that remove a filesystem object
  1260. * must be hooked prior, in order to capture the target inode during
  1261. * unsuccessful attempts.
  1262. */
  1263. void __audit_inode_child(const char *dname, const struct inode *inode,
  1264. const struct inode *parent)
  1265. {
  1266. int idx;
  1267. struct audit_context *context = current->audit_context;
  1268. const char *found_name = NULL;
  1269. int dirlen = 0;
  1270. if (!context->in_syscall)
  1271. return;
  1272. /* determine matching parent */
  1273. if (!dname)
  1274. goto update_context;
  1275. for (idx = 0; idx < context->name_count; idx++)
  1276. if (context->names[idx].ino == parent->i_ino) {
  1277. const char *name = context->names[idx].name;
  1278. if (!name)
  1279. continue;
  1280. if (audit_compare_dname_path(dname, name, &dirlen) == 0) {
  1281. context->names[idx].name_len = dirlen;
  1282. found_name = name;
  1283. break;
  1284. }
  1285. }
  1286. update_context:
  1287. idx = context->name_count;
  1288. if (context->name_count == AUDIT_NAMES) {
  1289. printk(KERN_DEBUG "name_count maxed and losing %s\n",
  1290. found_name ?: "(null)");
  1291. return;
  1292. }
  1293. context->name_count++;
  1294. #if AUDIT_DEBUG
  1295. context->ino_count++;
  1296. #endif
  1297. /* Re-use the name belonging to the slot for a matching parent directory.
  1298. * All names for this context are relinquished in audit_free_names() */
  1299. context->names[idx].name = found_name;
  1300. context->names[idx].name_len = AUDIT_NAME_FULL;
  1301. context->names[idx].name_put = 0; /* don't call __putname() */
  1302. if (!inode)
  1303. context->names[idx].ino = (unsigned long)-1;
  1304. else
  1305. audit_copy_inode(&context->names[idx], inode);
  1306. /* A parent was not found in audit_names, so copy the inode data for the
  1307. * provided parent. */
  1308. if (!found_name) {
  1309. idx = context->name_count;
  1310. if (context->name_count == AUDIT_NAMES) {
  1311. printk(KERN_DEBUG
  1312. "name_count maxed and losing parent inode data: dev=%02x:%02x, inode=%lu",
  1313. MAJOR(parent->i_sb->s_dev),
  1314. MINOR(parent->i_sb->s_dev),
  1315. parent->i_ino);
  1316. return;
  1317. }
  1318. context->name_count++;
  1319. #if AUDIT_DEBUG
  1320. context->ino_count++;
  1321. #endif
  1322. audit_copy_inode(&context->names[idx], parent);
  1323. }
  1324. }
  1325. /**
  1326. * auditsc_get_stamp - get local copies of audit_context values
  1327. * @ctx: audit_context for the task
  1328. * @t: timespec to store time recorded in the audit_context
  1329. * @serial: serial value that is recorded in the audit_context
  1330. *
  1331. * Also sets the context as auditable.
  1332. */
  1333. void auditsc_get_stamp(struct audit_context *ctx,
  1334. struct timespec *t, unsigned int *serial)
  1335. {
  1336. if (!ctx->serial)
  1337. ctx->serial = audit_serial();
  1338. t->tv_sec = ctx->ctime.tv_sec;
  1339. t->tv_nsec = ctx->ctime.tv_nsec;
  1340. *serial = ctx->serial;
  1341. ctx->auditable = 1;
  1342. }
  1343. /**
  1344. * audit_set_loginuid - set a task's audit_context loginuid
  1345. * @task: task whose audit context is being modified
  1346. * @loginuid: loginuid value
  1347. *
  1348. * Returns 0.
  1349. *
  1350. * Called (set) from fs/proc/base.c::proc_loginuid_write().
  1351. */
  1352. int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
  1353. {
  1354. struct audit_context *context = task->audit_context;
  1355. if (context) {
  1356. /* Only log if audit is enabled */
  1357. if (context->in_syscall) {
  1358. struct audit_buffer *ab;
  1359. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
  1360. if (ab) {
  1361. audit_log_format(ab, "login pid=%d uid=%u "
  1362. "old auid=%u new auid=%u",
  1363. task->pid, task->uid,
  1364. context->loginuid, loginuid);
  1365. audit_log_end(ab);
  1366. }
  1367. }
  1368. context->loginuid = loginuid;
  1369. }
  1370. return 0;
  1371. }
  1372. /**
  1373. * audit_get_loginuid - get the loginuid for an audit_context
  1374. * @ctx: the audit_context
  1375. *
  1376. * Returns the context's loginuid or -1 if @ctx is NULL.
  1377. */
  1378. uid_t audit_get_loginuid(struct audit_context *ctx)
  1379. {
  1380. return ctx ? ctx->loginuid : -1;
  1381. }
  1382. EXPORT_SYMBOL(audit_get_loginuid);
  1383. /**
  1384. * __audit_mq_open - record audit data for a POSIX MQ open
  1385. * @oflag: open flag
  1386. * @mode: mode bits
  1387. * @u_attr: queue attributes
  1388. *
  1389. * Returns 0 for success or NULL context or < 0 on error.
  1390. */
  1391. int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
  1392. {
  1393. struct audit_aux_data_mq_open *ax;
  1394. struct audit_context *context = current->audit_context;
  1395. if (!audit_enabled)
  1396. return 0;
  1397. if (likely(!context))
  1398. return 0;
  1399. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1400. if (!ax)
  1401. return -ENOMEM;
  1402. if (u_attr != NULL) {
  1403. if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
  1404. kfree(ax);
  1405. return -EFAULT;
  1406. }
  1407. } else
  1408. memset(&ax->attr, 0, sizeof(ax->attr));
  1409. ax->oflag = oflag;
  1410. ax->mode = mode;
  1411. ax->d.type = AUDIT_MQ_OPEN;
  1412. ax->d.next = context->aux;
  1413. context->aux = (void *)ax;
  1414. return 0;
  1415. }
  1416. /**
  1417. * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
  1418. * @mqdes: MQ descriptor
  1419. * @msg_len: Message length
  1420. * @msg_prio: Message priority
  1421. * @u_abs_timeout: Message timeout in absolute time
  1422. *
  1423. * Returns 0 for success or NULL context or < 0 on error.
  1424. */
  1425. int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
  1426. const struct timespec __user *u_abs_timeout)
  1427. {
  1428. struct audit_aux_data_mq_sendrecv *ax;
  1429. struct audit_context *context = current->audit_context;
  1430. if (!audit_enabled)
  1431. return 0;
  1432. if (likely(!context))
  1433. return 0;
  1434. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1435. if (!ax)
  1436. return -ENOMEM;
  1437. if (u_abs_timeout != NULL) {
  1438. if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
  1439. kfree(ax);
  1440. return -EFAULT;
  1441. }
  1442. } else
  1443. memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
  1444. ax->mqdes = mqdes;
  1445. ax->msg_len = msg_len;
  1446. ax->msg_prio = msg_prio;
  1447. ax->d.type = AUDIT_MQ_SENDRECV;
  1448. ax->d.next = context->aux;
  1449. context->aux = (void *)ax;
  1450. return 0;
  1451. }
  1452. /**
  1453. * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
  1454. * @mqdes: MQ descriptor
  1455. * @msg_len: Message length
  1456. * @u_msg_prio: Message priority
  1457. * @u_abs_timeout: Message timeout in absolute time
  1458. *
  1459. * Returns 0 for success or NULL context or < 0 on error.
  1460. */
  1461. int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
  1462. unsigned int __user *u_msg_prio,
  1463. const struct timespec __user *u_abs_timeout)
  1464. {
  1465. struct audit_aux_data_mq_sendrecv *ax;
  1466. struct audit_context *context = current->audit_context;
  1467. if (!audit_enabled)
  1468. return 0;
  1469. if (likely(!context))
  1470. return 0;
  1471. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1472. if (!ax)
  1473. return -ENOMEM;
  1474. if (u_msg_prio != NULL) {
  1475. if (get_user(ax->msg_prio, u_msg_prio)) {
  1476. kfree(ax);
  1477. return -EFAULT;
  1478. }
  1479. } else
  1480. ax->msg_prio = 0;
  1481. if (u_abs_timeout != NULL) {
  1482. if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
  1483. kfree(ax);
  1484. return -EFAULT;
  1485. }
  1486. } else
  1487. memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
  1488. ax->mqdes = mqdes;
  1489. ax->msg_len = msg_len;
  1490. ax->d.type = AUDIT_MQ_SENDRECV;
  1491. ax->d.next = context->aux;
  1492. context->aux = (void *)ax;
  1493. return 0;
  1494. }
  1495. /**
  1496. * __audit_mq_notify - record audit data for a POSIX MQ notify
  1497. * @mqdes: MQ descriptor
  1498. * @u_notification: Notification event
  1499. *
  1500. * Returns 0 for success or NULL context or < 0 on error.
  1501. */
  1502. int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
  1503. {
  1504. struct audit_aux_data_mq_notify *ax;
  1505. struct audit_context *context = current->audit_context;
  1506. if (!audit_enabled)
  1507. return 0;
  1508. if (likely(!context))
  1509. return 0;
  1510. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1511. if (!ax)
  1512. return -ENOMEM;
  1513. if (u_notification != NULL) {
  1514. if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
  1515. kfree(ax);
  1516. return -EFAULT;
  1517. }
  1518. } else
  1519. memset(&ax->notification, 0, sizeof(ax->notification));
  1520. ax->mqdes = mqdes;
  1521. ax->d.type = AUDIT_MQ_NOTIFY;
  1522. ax->d.next = context->aux;
  1523. context->aux = (void *)ax;
  1524. return 0;
  1525. }
  1526. /**
  1527. * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
  1528. * @mqdes: MQ descriptor
  1529. * @mqstat: MQ flags
  1530. *
  1531. * Returns 0 for success or NULL context or < 0 on error.
  1532. */
  1533. int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
  1534. {
  1535. struct audit_aux_data_mq_getsetattr *ax;
  1536. struct audit_context *context = current->audit_context;
  1537. if (!audit_enabled)
  1538. return 0;
  1539. if (likely(!context))
  1540. return 0;
  1541. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1542. if (!ax)
  1543. return -ENOMEM;
  1544. ax->mqdes = mqdes;
  1545. ax->mqstat = *mqstat;
  1546. ax->d.type = AUDIT_MQ_GETSETATTR;
  1547. ax->d.next = context->aux;
  1548. context->aux = (void *)ax;
  1549. return 0;
  1550. }
  1551. /**
  1552. * audit_ipc_obj - record audit data for ipc object
  1553. * @ipcp: ipc permissions
  1554. *
  1555. * Returns 0 for success or NULL context or < 0 on error.
  1556. */
  1557. int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
  1558. {
  1559. struct audit_aux_data_ipcctl *ax;
  1560. struct audit_context *context = current->audit_context;
  1561. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1562. if (!ax)
  1563. return -ENOMEM;
  1564. ax->uid = ipcp->uid;
  1565. ax->gid = ipcp->gid;
  1566. ax->mode = ipcp->mode;
  1567. selinux_get_ipc_sid(ipcp, &ax->osid);
  1568. ax->d.type = AUDIT_IPC;
  1569. ax->d.next = context->aux;
  1570. context->aux = (void *)ax;
  1571. return 0;
  1572. }
  1573. /**
  1574. * audit_ipc_set_perm - record audit data for new ipc permissions
  1575. * @qbytes: msgq bytes
  1576. * @uid: msgq user id
  1577. * @gid: msgq group id
  1578. * @mode: msgq mode (permissions)
  1579. *
  1580. * Returns 0 for success or NULL context or < 0 on error.
  1581. */
  1582. int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
  1583. {
  1584. struct audit_aux_data_ipcctl *ax;
  1585. struct audit_context *context = current->audit_context;
  1586. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1587. if (!ax)
  1588. return -ENOMEM;
  1589. ax->qbytes = qbytes;
  1590. ax->uid = uid;
  1591. ax->gid = gid;
  1592. ax->mode = mode;
  1593. ax->d.type = AUDIT_IPC_SET_PERM;
  1594. ax->d.next = context->aux;
  1595. context->aux = (void *)ax;
  1596. return 0;
  1597. }
  1598. int audit_bprm(struct linux_binprm *bprm)
  1599. {
  1600. struct audit_aux_data_execve *ax;
  1601. struct audit_context *context = current->audit_context;
  1602. unsigned long p, next;
  1603. void *to;
  1604. if (likely(!audit_enabled || !context || context->dummy))
  1605. return 0;
  1606. ax = kmalloc(sizeof(*ax) + PAGE_SIZE * MAX_ARG_PAGES - bprm->p,
  1607. GFP_KERNEL);
  1608. if (!ax)
  1609. return -ENOMEM;
  1610. ax->argc = bprm->argc;
  1611. ax->envc = bprm->envc;
  1612. for (p = bprm->p, to = ax->mem; p < MAX_ARG_PAGES*PAGE_SIZE; p = next) {
  1613. struct page *page = bprm->page[p / PAGE_SIZE];
  1614. void *kaddr = kmap(page);
  1615. next = (p + PAGE_SIZE) & ~(PAGE_SIZE - 1);
  1616. memcpy(to, kaddr + (p & (PAGE_SIZE - 1)), next - p);
  1617. to += next - p;
  1618. kunmap(page);
  1619. }
  1620. ax->d.type = AUDIT_EXECVE;
  1621. ax->d.next = context->aux;
  1622. context->aux = (void *)ax;
  1623. return 0;
  1624. }
  1625. /**
  1626. * audit_socketcall - record audit data for sys_socketcall
  1627. * @nargs: number of args
  1628. * @args: args array
  1629. *
  1630. * Returns 0 for success or NULL context or < 0 on error.
  1631. */
  1632. int audit_socketcall(int nargs, unsigned long *args)
  1633. {
  1634. struct audit_aux_data_socketcall *ax;
  1635. struct audit_context *context = current->audit_context;
  1636. if (likely(!context || context->dummy))
  1637. return 0;
  1638. ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
  1639. if (!ax)
  1640. return -ENOMEM;
  1641. ax->nargs = nargs;
  1642. memcpy(ax->args, args, nargs * sizeof(unsigned long));
  1643. ax->d.type = AUDIT_SOCKETCALL;
  1644. ax->d.next = context->aux;
  1645. context->aux = (void *)ax;
  1646. return 0;
  1647. }
  1648. /**
  1649. * __audit_fd_pair - record audit data for pipe and socketpair
  1650. * @fd1: the first file descriptor
  1651. * @fd2: the second file descriptor
  1652. *
  1653. * Returns 0 for success or NULL context or < 0 on error.
  1654. */
  1655. int __audit_fd_pair(int fd1, int fd2)
  1656. {
  1657. struct audit_context *context = current->audit_context;
  1658. struct audit_aux_data_fd_pair *ax;
  1659. if (likely(!context)) {
  1660. return 0;
  1661. }
  1662. ax = kmalloc(sizeof(*ax), GFP_KERNEL);
  1663. if (!ax) {
  1664. return -ENOMEM;
  1665. }
  1666. ax->fd[0] = fd1;
  1667. ax->fd[1] = fd2;
  1668. ax->d.type = AUDIT_FD_PAIR;
  1669. ax->d.next = context->aux;
  1670. context->aux = (void *)ax;
  1671. return 0;
  1672. }
  1673. /**
  1674. * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
  1675. * @len: data length in user space
  1676. * @a: data address in kernel space
  1677. *
  1678. * Returns 0 for success or NULL context or < 0 on error.
  1679. */
  1680. int audit_sockaddr(int len, void *a)
  1681. {
  1682. struct audit_aux_data_sockaddr *ax;
  1683. struct audit_context *context = current->audit_context;
  1684. if (likely(!context || context->dummy))
  1685. return 0;
  1686. ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
  1687. if (!ax)
  1688. return -ENOMEM;
  1689. ax->len = len;
  1690. memcpy(ax->a, a, len);
  1691. ax->d.type = AUDIT_SOCKADDR;
  1692. ax->d.next = context->aux;
  1693. context->aux = (void *)ax;
  1694. return 0;
  1695. }
  1696. void __audit_ptrace(struct task_struct *t)
  1697. {
  1698. struct audit_context *context = current->audit_context;
  1699. context->target_pid = t->pid;
  1700. selinux_get_task_sid(t, &context->target_sid);
  1701. }
  1702. /**
  1703. * audit_avc_path - record the granting or denial of permissions
  1704. * @dentry: dentry to record
  1705. * @mnt: mnt to record
  1706. *
  1707. * Returns 0 for success or NULL context or < 0 on error.
  1708. *
  1709. * Called from security/selinux/avc.c::avc_audit()
  1710. */
  1711. int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
  1712. {
  1713. struct audit_aux_data_path *ax;
  1714. struct audit_context *context = current->audit_context;
  1715. if (likely(!context))
  1716. return 0;
  1717. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1718. if (!ax)
  1719. return -ENOMEM;
  1720. ax->dentry = dget(dentry);
  1721. ax->mnt = mntget(mnt);
  1722. ax->d.type = AUDIT_AVC_PATH;
  1723. ax->d.next = context->aux;
  1724. context->aux = (void *)ax;
  1725. return 0;
  1726. }
  1727. /**
  1728. * audit_signal_info - record signal info for shutting down audit subsystem
  1729. * @sig: signal value
  1730. * @t: task being signaled
  1731. *
  1732. * If the audit subsystem is being terminated, record the task (pid)
  1733. * and uid that is doing that.
  1734. */
  1735. int __audit_signal_info(int sig, struct task_struct *t)
  1736. {
  1737. struct audit_aux_data_pids *axp;
  1738. struct task_struct *tsk = current;
  1739. struct audit_context *ctx = tsk->audit_context;
  1740. extern pid_t audit_sig_pid;
  1741. extern uid_t audit_sig_uid;
  1742. extern u32 audit_sig_sid;
  1743. if (audit_pid && t->tgid == audit_pid &&
  1744. (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1)) {
  1745. audit_sig_pid = tsk->pid;
  1746. if (ctx)
  1747. audit_sig_uid = ctx->loginuid;
  1748. else
  1749. audit_sig_uid = tsk->uid;
  1750. selinux_get_task_sid(tsk, &audit_sig_sid);
  1751. }
  1752. if (!audit_signals) /* audit_context checked in wrapper */
  1753. return 0;
  1754. /* optimize the common case by putting first signal recipient directly
  1755. * in audit_context */
  1756. if (!ctx->target_pid) {
  1757. ctx->target_pid = t->tgid;
  1758. selinux_get_task_sid(t, &ctx->target_sid);
  1759. return 0;
  1760. }
  1761. axp = (void *)ctx->aux_pids;
  1762. if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
  1763. axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
  1764. if (!axp)
  1765. return -ENOMEM;
  1766. axp->d.type = AUDIT_OBJ_PID;
  1767. axp->d.next = ctx->aux_pids;
  1768. ctx->aux_pids = (void *)axp;
  1769. }
  1770. BUG_ON(axp->pid_count > AUDIT_AUX_PIDS);
  1771. axp->target_pid[axp->pid_count] = t->tgid;
  1772. selinux_get_task_sid(t, &axp->target_sid[axp->pid_count]);
  1773. axp->pid_count++;
  1774. return 0;
  1775. }