auditsc.c 44 KB

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