auditsc.c 49 KB

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