auditsc.c 48 KB

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