auditsc.c 46 KB

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