auditsc.c 44 KB

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