auditsc.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893
  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. static 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. static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
  689. {
  690. char name[sizeof(tsk->comm)];
  691. struct mm_struct *mm = tsk->mm;
  692. struct vm_area_struct *vma;
  693. /* tsk == current */
  694. get_task_comm(name, tsk);
  695. audit_log_format(ab, " comm=");
  696. audit_log_untrustedstring(ab, name);
  697. if (mm) {
  698. down_read(&mm->mmap_sem);
  699. vma = mm->mmap;
  700. while (vma) {
  701. if ((vma->vm_flags & VM_EXECUTABLE) &&
  702. vma->vm_file) {
  703. audit_log_d_path(ab, "exe=",
  704. vma->vm_file->f_dentry,
  705. vma->vm_file->f_vfsmnt);
  706. break;
  707. }
  708. vma = vma->vm_next;
  709. }
  710. up_read(&mm->mmap_sem);
  711. }
  712. audit_log_task_context(ab);
  713. }
  714. static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
  715. {
  716. int i, call_panic = 0;
  717. struct audit_buffer *ab;
  718. struct audit_aux_data *aux;
  719. const char *tty;
  720. /* tsk == current */
  721. context->pid = tsk->pid;
  722. if (!context->ppid)
  723. context->ppid = sys_getppid();
  724. context->uid = tsk->uid;
  725. context->gid = tsk->gid;
  726. context->euid = tsk->euid;
  727. context->suid = tsk->suid;
  728. context->fsuid = tsk->fsuid;
  729. context->egid = tsk->egid;
  730. context->sgid = tsk->sgid;
  731. context->fsgid = tsk->fsgid;
  732. context->personality = tsk->personality;
  733. ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
  734. if (!ab)
  735. return; /* audit_panic has been called */
  736. audit_log_format(ab, "arch=%x syscall=%d",
  737. context->arch, context->major);
  738. if (context->personality != PER_LINUX)
  739. audit_log_format(ab, " per=%lx", context->personality);
  740. if (context->return_valid)
  741. audit_log_format(ab, " success=%s exit=%ld",
  742. (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
  743. context->return_code);
  744. mutex_lock(&tty_mutex);
  745. if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
  746. tty = tsk->signal->tty->name;
  747. else
  748. tty = "(none)";
  749. audit_log_format(ab,
  750. " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
  751. " ppid=%d pid=%d auid=%u uid=%u gid=%u"
  752. " euid=%u suid=%u fsuid=%u"
  753. " egid=%u sgid=%u fsgid=%u tty=%s",
  754. context->argv[0],
  755. context->argv[1],
  756. context->argv[2],
  757. context->argv[3],
  758. context->name_count,
  759. context->ppid,
  760. context->pid,
  761. context->loginuid,
  762. context->uid,
  763. context->gid,
  764. context->euid, context->suid, context->fsuid,
  765. context->egid, context->sgid, context->fsgid, tty);
  766. mutex_unlock(&tty_mutex);
  767. audit_log_task_info(ab, tsk);
  768. if (context->filterkey) {
  769. audit_log_format(ab, " key=");
  770. audit_log_untrustedstring(ab, context->filterkey);
  771. } else
  772. audit_log_format(ab, " key=(null)");
  773. audit_log_end(ab);
  774. for (aux = context->aux; aux; aux = aux->next) {
  775. ab = audit_log_start(context, GFP_KERNEL, aux->type);
  776. if (!ab)
  777. continue; /* audit_panic has been called */
  778. switch (aux->type) {
  779. case AUDIT_MQ_OPEN: {
  780. struct audit_aux_data_mq_open *axi = (void *)aux;
  781. audit_log_format(ab,
  782. "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
  783. "mq_msgsize=%ld mq_curmsgs=%ld",
  784. axi->oflag, axi->mode, axi->attr.mq_flags,
  785. axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
  786. axi->attr.mq_curmsgs);
  787. break; }
  788. case AUDIT_MQ_SENDRECV: {
  789. struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
  790. audit_log_format(ab,
  791. "mqdes=%d msg_len=%zd msg_prio=%u "
  792. "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
  793. axi->mqdes, axi->msg_len, axi->msg_prio,
  794. axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
  795. break; }
  796. case AUDIT_MQ_NOTIFY: {
  797. struct audit_aux_data_mq_notify *axi = (void *)aux;
  798. audit_log_format(ab,
  799. "mqdes=%d sigev_signo=%d",
  800. axi->mqdes,
  801. axi->notification.sigev_signo);
  802. break; }
  803. case AUDIT_MQ_GETSETATTR: {
  804. struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
  805. audit_log_format(ab,
  806. "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
  807. "mq_curmsgs=%ld ",
  808. axi->mqdes,
  809. axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
  810. axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
  811. break; }
  812. case AUDIT_IPC: {
  813. struct audit_aux_data_ipcctl *axi = (void *)aux;
  814. audit_log_format(ab,
  815. "ouid=%u ogid=%u mode=%x",
  816. axi->uid, axi->gid, axi->mode);
  817. if (axi->osid != 0) {
  818. char *ctx = NULL;
  819. u32 len;
  820. if (selinux_sid_to_string(
  821. axi->osid, &ctx, &len)) {
  822. audit_log_format(ab, " osid=%u",
  823. axi->osid);
  824. call_panic = 1;
  825. } else
  826. audit_log_format(ab, " obj=%s", ctx);
  827. kfree(ctx);
  828. }
  829. break; }
  830. case AUDIT_IPC_SET_PERM: {
  831. struct audit_aux_data_ipcctl *axi = (void *)aux;
  832. audit_log_format(ab,
  833. "qbytes=%lx ouid=%u ogid=%u mode=%x",
  834. axi->qbytes, axi->uid, axi->gid, axi->mode);
  835. break; }
  836. case AUDIT_EXECVE: {
  837. struct audit_aux_data_execve *axi = (void *)aux;
  838. int i;
  839. const char *p;
  840. for (i = 0, p = axi->mem; i < axi->argc; i++) {
  841. audit_log_format(ab, "a%d=", i);
  842. p = audit_log_untrustedstring(ab, p);
  843. audit_log_format(ab, "\n");
  844. }
  845. break; }
  846. case AUDIT_SOCKETCALL: {
  847. int i;
  848. struct audit_aux_data_socketcall *axs = (void *)aux;
  849. audit_log_format(ab, "nargs=%d", axs->nargs);
  850. for (i=0; i<axs->nargs; i++)
  851. audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
  852. break; }
  853. case AUDIT_SOCKADDR: {
  854. struct audit_aux_data_sockaddr *axs = (void *)aux;
  855. audit_log_format(ab, "saddr=");
  856. audit_log_hex(ab, axs->a, axs->len);
  857. break; }
  858. case AUDIT_AVC_PATH: {
  859. struct audit_aux_data_path *axi = (void *)aux;
  860. audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
  861. break; }
  862. }
  863. audit_log_end(ab);
  864. }
  865. if (context->pwd && context->pwdmnt) {
  866. ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
  867. if (ab) {
  868. audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
  869. audit_log_end(ab);
  870. }
  871. }
  872. for (i = 0; i < context->name_count; i++) {
  873. struct audit_names *n = &context->names[i];
  874. ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
  875. if (!ab)
  876. continue; /* audit_panic has been called */
  877. audit_log_format(ab, "item=%d", i);
  878. if (n->name) {
  879. switch(n->name_len) {
  880. case AUDIT_NAME_FULL:
  881. /* log the full path */
  882. audit_log_format(ab, " name=");
  883. audit_log_untrustedstring(ab, n->name);
  884. break;
  885. case 0:
  886. /* name was specified as a relative path and the
  887. * directory component is the cwd */
  888. audit_log_d_path(ab, " name=", context->pwd,
  889. context->pwdmnt);
  890. break;
  891. default:
  892. /* log the name's directory component */
  893. audit_log_format(ab, " name=");
  894. audit_log_n_untrustedstring(ab, n->name_len,
  895. n->name);
  896. }
  897. } else
  898. audit_log_format(ab, " name=(null)");
  899. if (n->ino != (unsigned long)-1) {
  900. audit_log_format(ab, " inode=%lu"
  901. " dev=%02x:%02x mode=%#o"
  902. " ouid=%u ogid=%u rdev=%02x:%02x",
  903. n->ino,
  904. MAJOR(n->dev),
  905. MINOR(n->dev),
  906. n->mode,
  907. n->uid,
  908. n->gid,
  909. MAJOR(n->rdev),
  910. MINOR(n->rdev));
  911. }
  912. if (n->osid != 0) {
  913. char *ctx = NULL;
  914. u32 len;
  915. if (selinux_sid_to_string(
  916. n->osid, &ctx, &len)) {
  917. audit_log_format(ab, " osid=%u", n->osid);
  918. call_panic = 2;
  919. } else
  920. audit_log_format(ab, " obj=%s", ctx);
  921. kfree(ctx);
  922. }
  923. audit_log_end(ab);
  924. }
  925. if (call_panic)
  926. audit_panic("error converting sid to string");
  927. }
  928. /**
  929. * audit_free - free a per-task audit context
  930. * @tsk: task whose audit context block to free
  931. *
  932. * Called from copy_process and do_exit
  933. */
  934. void audit_free(struct task_struct *tsk)
  935. {
  936. struct audit_context *context;
  937. context = audit_get_context(tsk, 0, 0);
  938. if (likely(!context))
  939. return;
  940. /* Check for system calls that do not go through the exit
  941. * function (e.g., exit_group), then free context block.
  942. * We use GFP_ATOMIC here because we might be doing this
  943. * in the context of the idle thread */
  944. /* that can happen only if we are called from do_exit() */
  945. if (context->in_syscall && context->auditable)
  946. audit_log_exit(context, tsk);
  947. audit_free_context(context);
  948. }
  949. /**
  950. * audit_syscall_entry - fill in an audit record at syscall entry
  951. * @tsk: task being audited
  952. * @arch: architecture type
  953. * @major: major syscall type (function)
  954. * @a1: additional syscall register 1
  955. * @a2: additional syscall register 2
  956. * @a3: additional syscall register 3
  957. * @a4: additional syscall register 4
  958. *
  959. * Fill in audit context at syscall entry. This only happens if the
  960. * audit context was created when the task was created and the state or
  961. * filters demand the audit context be built. If the state from the
  962. * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
  963. * then the record will be written at syscall exit time (otherwise, it
  964. * will only be written if another part of the kernel requests that it
  965. * be written).
  966. */
  967. void audit_syscall_entry(int arch, int major,
  968. unsigned long a1, unsigned long a2,
  969. unsigned long a3, unsigned long a4)
  970. {
  971. struct task_struct *tsk = current;
  972. struct audit_context *context = tsk->audit_context;
  973. enum audit_state state;
  974. BUG_ON(!context);
  975. /*
  976. * This happens only on certain architectures that make system
  977. * calls in kernel_thread via the entry.S interface, instead of
  978. * with direct calls. (If you are porting to a new
  979. * architecture, hitting this condition can indicate that you
  980. * got the _exit/_leave calls backward in entry.S.)
  981. *
  982. * i386 no
  983. * x86_64 no
  984. * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
  985. *
  986. * This also happens with vm86 emulation in a non-nested manner
  987. * (entries without exits), so this case must be caught.
  988. */
  989. if (context->in_syscall) {
  990. struct audit_context *newctx;
  991. #if AUDIT_DEBUG
  992. printk(KERN_ERR
  993. "audit(:%d) pid=%d in syscall=%d;"
  994. " entering syscall=%d\n",
  995. context->serial, tsk->pid, context->major, major);
  996. #endif
  997. newctx = audit_alloc_context(context->state);
  998. if (newctx) {
  999. newctx->previous = context;
  1000. context = newctx;
  1001. tsk->audit_context = newctx;
  1002. } else {
  1003. /* If we can't alloc a new context, the best we
  1004. * can do is to leak memory (any pending putname
  1005. * will be lost). The only other alternative is
  1006. * to abandon auditing. */
  1007. audit_zero_context(context, context->state);
  1008. }
  1009. }
  1010. BUG_ON(context->in_syscall || context->name_count);
  1011. if (!audit_enabled)
  1012. return;
  1013. context->arch = arch;
  1014. context->major = major;
  1015. context->argv[0] = a1;
  1016. context->argv[1] = a2;
  1017. context->argv[2] = a3;
  1018. context->argv[3] = a4;
  1019. state = context->state;
  1020. context->dummy = !audit_n_rules;
  1021. if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT))
  1022. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
  1023. if (likely(state == AUDIT_DISABLED))
  1024. return;
  1025. context->serial = 0;
  1026. context->ctime = CURRENT_TIME;
  1027. context->in_syscall = 1;
  1028. context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
  1029. context->ppid = 0;
  1030. }
  1031. /**
  1032. * audit_syscall_exit - deallocate audit context after a system call
  1033. * @tsk: task being audited
  1034. * @valid: success/failure flag
  1035. * @return_code: syscall return value
  1036. *
  1037. * Tear down after system call. If the audit context has been marked as
  1038. * auditable (either because of the AUDIT_RECORD_CONTEXT state from
  1039. * filtering, or because some other part of the kernel write an audit
  1040. * message), then write out the syscall information. In call cases,
  1041. * free the names stored from getname().
  1042. */
  1043. void audit_syscall_exit(int valid, long return_code)
  1044. {
  1045. struct task_struct *tsk = current;
  1046. struct audit_context *context;
  1047. context = audit_get_context(tsk, valid, return_code);
  1048. if (likely(!context))
  1049. return;
  1050. if (context->in_syscall && context->auditable)
  1051. audit_log_exit(context, tsk);
  1052. context->in_syscall = 0;
  1053. context->auditable = 0;
  1054. if (context->previous) {
  1055. struct audit_context *new_context = context->previous;
  1056. context->previous = NULL;
  1057. audit_free_context(context);
  1058. tsk->audit_context = new_context;
  1059. } else {
  1060. audit_free_names(context);
  1061. audit_free_aux(context);
  1062. kfree(context->filterkey);
  1063. context->filterkey = NULL;
  1064. tsk->audit_context = context;
  1065. }
  1066. }
  1067. /**
  1068. * audit_getname - add a name to the list
  1069. * @name: name to add
  1070. *
  1071. * Add a name to the list of audit names for this context.
  1072. * Called from fs/namei.c:getname().
  1073. */
  1074. void __audit_getname(const char *name)
  1075. {
  1076. struct audit_context *context = current->audit_context;
  1077. if (IS_ERR(name) || !name)
  1078. return;
  1079. if (!context->in_syscall) {
  1080. #if AUDIT_DEBUG == 2
  1081. printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
  1082. __FILE__, __LINE__, context->serial, name);
  1083. dump_stack();
  1084. #endif
  1085. return;
  1086. }
  1087. BUG_ON(context->name_count >= AUDIT_NAMES);
  1088. context->names[context->name_count].name = name;
  1089. context->names[context->name_count].name_len = AUDIT_NAME_FULL;
  1090. context->names[context->name_count].name_put = 1;
  1091. context->names[context->name_count].ino = (unsigned long)-1;
  1092. ++context->name_count;
  1093. if (!context->pwd) {
  1094. read_lock(&current->fs->lock);
  1095. context->pwd = dget(current->fs->pwd);
  1096. context->pwdmnt = mntget(current->fs->pwdmnt);
  1097. read_unlock(&current->fs->lock);
  1098. }
  1099. }
  1100. /* audit_putname - intercept a putname request
  1101. * @name: name to intercept and delay for putname
  1102. *
  1103. * If we have stored the name from getname in the audit context,
  1104. * then we delay the putname until syscall exit.
  1105. * Called from include/linux/fs.h:putname().
  1106. */
  1107. void audit_putname(const char *name)
  1108. {
  1109. struct audit_context *context = current->audit_context;
  1110. BUG_ON(!context);
  1111. if (!context->in_syscall) {
  1112. #if AUDIT_DEBUG == 2
  1113. printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
  1114. __FILE__, __LINE__, context->serial, name);
  1115. if (context->name_count) {
  1116. int i;
  1117. for (i = 0; i < context->name_count; i++)
  1118. printk(KERN_ERR "name[%d] = %p = %s\n", i,
  1119. context->names[i].name,
  1120. context->names[i].name ?: "(null)");
  1121. }
  1122. #endif
  1123. __putname(name);
  1124. }
  1125. #if AUDIT_DEBUG
  1126. else {
  1127. ++context->put_count;
  1128. if (context->put_count > context->name_count) {
  1129. printk(KERN_ERR "%s:%d(:%d): major=%d"
  1130. " in_syscall=%d putname(%p) name_count=%d"
  1131. " put_count=%d\n",
  1132. __FILE__, __LINE__,
  1133. context->serial, context->major,
  1134. context->in_syscall, name, context->name_count,
  1135. context->put_count);
  1136. dump_stack();
  1137. }
  1138. }
  1139. #endif
  1140. }
  1141. /* Copy inode data into an audit_names. */
  1142. static void audit_copy_inode(struct audit_names *name, const struct inode *inode)
  1143. {
  1144. name->ino = inode->i_ino;
  1145. name->dev = inode->i_sb->s_dev;
  1146. name->mode = inode->i_mode;
  1147. name->uid = inode->i_uid;
  1148. name->gid = inode->i_gid;
  1149. name->rdev = inode->i_rdev;
  1150. selinux_get_inode_sid(inode, &name->osid);
  1151. }
  1152. /**
  1153. * audit_inode - store the inode and device from a lookup
  1154. * @name: name being audited
  1155. * @inode: inode being audited
  1156. *
  1157. * Called from fs/namei.c:path_lookup().
  1158. */
  1159. void __audit_inode(const char *name, const struct inode *inode)
  1160. {
  1161. int idx;
  1162. struct audit_context *context = current->audit_context;
  1163. if (!context->in_syscall)
  1164. return;
  1165. if (context->name_count
  1166. && context->names[context->name_count-1].name
  1167. && context->names[context->name_count-1].name == name)
  1168. idx = context->name_count - 1;
  1169. else if (context->name_count > 1
  1170. && context->names[context->name_count-2].name
  1171. && context->names[context->name_count-2].name == name)
  1172. idx = context->name_count - 2;
  1173. else {
  1174. /* FIXME: how much do we care about inodes that have no
  1175. * associated name? */
  1176. if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
  1177. return;
  1178. idx = context->name_count++;
  1179. context->names[idx].name = NULL;
  1180. #if AUDIT_DEBUG
  1181. ++context->ino_count;
  1182. #endif
  1183. }
  1184. audit_copy_inode(&context->names[idx], inode);
  1185. }
  1186. /**
  1187. * audit_inode_child - collect inode info for created/removed objects
  1188. * @dname: inode's dentry name
  1189. * @inode: inode being audited
  1190. * @parent: inode of dentry parent
  1191. *
  1192. * For syscalls that create or remove filesystem objects, audit_inode
  1193. * can only collect information for the filesystem object's parent.
  1194. * This call updates the audit context with the child's information.
  1195. * Syscalls that create a new filesystem object must be hooked after
  1196. * the object is created. Syscalls that remove a filesystem object
  1197. * must be hooked prior, in order to capture the target inode during
  1198. * unsuccessful attempts.
  1199. */
  1200. void __audit_inode_child(const char *dname, const struct inode *inode,
  1201. const struct inode *parent)
  1202. {
  1203. int idx;
  1204. struct audit_context *context = current->audit_context;
  1205. const char *found_name = NULL;
  1206. int dirlen = 0;
  1207. if (!context->in_syscall)
  1208. return;
  1209. /* determine matching parent */
  1210. if (!dname)
  1211. goto update_context;
  1212. for (idx = 0; idx < context->name_count; idx++)
  1213. if (context->names[idx].ino == parent->i_ino) {
  1214. const char *name = context->names[idx].name;
  1215. if (!name)
  1216. continue;
  1217. if (audit_compare_dname_path(dname, name, &dirlen) == 0) {
  1218. context->names[idx].name_len = dirlen;
  1219. found_name = name;
  1220. break;
  1221. }
  1222. }
  1223. update_context:
  1224. idx = context->name_count;
  1225. if (context->name_count == AUDIT_NAMES) {
  1226. printk(KERN_DEBUG "name_count maxed and losing %s\n",
  1227. found_name ?: "(null)");
  1228. return;
  1229. }
  1230. context->name_count++;
  1231. #if AUDIT_DEBUG
  1232. context->ino_count++;
  1233. #endif
  1234. /* Re-use the name belonging to the slot for a matching parent directory.
  1235. * All names for this context are relinquished in audit_free_names() */
  1236. context->names[idx].name = found_name;
  1237. context->names[idx].name_len = AUDIT_NAME_FULL;
  1238. context->names[idx].name_put = 0; /* don't call __putname() */
  1239. if (!inode)
  1240. context->names[idx].ino = (unsigned long)-1;
  1241. else
  1242. audit_copy_inode(&context->names[idx], inode);
  1243. /* A parent was not found in audit_names, so copy the inode data for the
  1244. * provided parent. */
  1245. if (!found_name) {
  1246. idx = context->name_count;
  1247. if (context->name_count == AUDIT_NAMES) {
  1248. printk(KERN_DEBUG
  1249. "name_count maxed and losing parent inode data: dev=%02x:%02x, inode=%lu",
  1250. MAJOR(parent->i_sb->s_dev),
  1251. MINOR(parent->i_sb->s_dev),
  1252. parent->i_ino);
  1253. return;
  1254. }
  1255. context->name_count++;
  1256. #if AUDIT_DEBUG
  1257. context->ino_count++;
  1258. #endif
  1259. audit_copy_inode(&context->names[idx], parent);
  1260. }
  1261. }
  1262. /**
  1263. * audit_inode_update - update inode info for last collected name
  1264. * @inode: inode being audited
  1265. *
  1266. * When open() is called on an existing object with the O_CREAT flag, the inode
  1267. * data audit initially collects is incorrect. This additional hook ensures
  1268. * audit has the inode data for the actual object to be opened.
  1269. */
  1270. void __audit_inode_update(const struct inode *inode)
  1271. {
  1272. struct audit_context *context = current->audit_context;
  1273. int idx;
  1274. if (!context->in_syscall || !inode)
  1275. return;
  1276. if (context->name_count == 0) {
  1277. context->name_count++;
  1278. #if AUDIT_DEBUG
  1279. context->ino_count++;
  1280. #endif
  1281. }
  1282. idx = context->name_count - 1;
  1283. audit_copy_inode(&context->names[idx], inode);
  1284. }
  1285. /**
  1286. * auditsc_get_stamp - get local copies of audit_context values
  1287. * @ctx: audit_context for the task
  1288. * @t: timespec to store time recorded in the audit_context
  1289. * @serial: serial value that is recorded in the audit_context
  1290. *
  1291. * Also sets the context as auditable.
  1292. */
  1293. void auditsc_get_stamp(struct audit_context *ctx,
  1294. struct timespec *t, unsigned int *serial)
  1295. {
  1296. if (!ctx->serial)
  1297. ctx->serial = audit_serial();
  1298. t->tv_sec = ctx->ctime.tv_sec;
  1299. t->tv_nsec = ctx->ctime.tv_nsec;
  1300. *serial = ctx->serial;
  1301. ctx->auditable = 1;
  1302. }
  1303. /**
  1304. * audit_set_loginuid - set a task's audit_context loginuid
  1305. * @task: task whose audit context is being modified
  1306. * @loginuid: loginuid value
  1307. *
  1308. * Returns 0.
  1309. *
  1310. * Called (set) from fs/proc/base.c::proc_loginuid_write().
  1311. */
  1312. int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
  1313. {
  1314. struct audit_context *context = task->audit_context;
  1315. if (context) {
  1316. /* Only log if audit is enabled */
  1317. if (context->in_syscall) {
  1318. struct audit_buffer *ab;
  1319. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
  1320. if (ab) {
  1321. audit_log_format(ab, "login pid=%d uid=%u "
  1322. "old auid=%u new auid=%u",
  1323. task->pid, task->uid,
  1324. context->loginuid, loginuid);
  1325. audit_log_end(ab);
  1326. }
  1327. }
  1328. context->loginuid = loginuid;
  1329. }
  1330. return 0;
  1331. }
  1332. /**
  1333. * audit_get_loginuid - get the loginuid for an audit_context
  1334. * @ctx: the audit_context
  1335. *
  1336. * Returns the context's loginuid or -1 if @ctx is NULL.
  1337. */
  1338. uid_t audit_get_loginuid(struct audit_context *ctx)
  1339. {
  1340. return ctx ? ctx->loginuid : -1;
  1341. }
  1342. /**
  1343. * __audit_mq_open - record audit data for a POSIX MQ open
  1344. * @oflag: open flag
  1345. * @mode: mode bits
  1346. * @u_attr: queue attributes
  1347. *
  1348. * Returns 0 for success or NULL context or < 0 on error.
  1349. */
  1350. int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
  1351. {
  1352. struct audit_aux_data_mq_open *ax;
  1353. struct audit_context *context = current->audit_context;
  1354. if (!audit_enabled)
  1355. return 0;
  1356. if (likely(!context))
  1357. return 0;
  1358. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1359. if (!ax)
  1360. return -ENOMEM;
  1361. if (u_attr != NULL) {
  1362. if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
  1363. kfree(ax);
  1364. return -EFAULT;
  1365. }
  1366. } else
  1367. memset(&ax->attr, 0, sizeof(ax->attr));
  1368. ax->oflag = oflag;
  1369. ax->mode = mode;
  1370. ax->d.type = AUDIT_MQ_OPEN;
  1371. ax->d.next = context->aux;
  1372. context->aux = (void *)ax;
  1373. return 0;
  1374. }
  1375. /**
  1376. * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
  1377. * @mqdes: MQ descriptor
  1378. * @msg_len: Message length
  1379. * @msg_prio: Message priority
  1380. * @u_abs_timeout: Message timeout in absolute time
  1381. *
  1382. * Returns 0 for success or NULL context or < 0 on error.
  1383. */
  1384. int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
  1385. const struct timespec __user *u_abs_timeout)
  1386. {
  1387. struct audit_aux_data_mq_sendrecv *ax;
  1388. struct audit_context *context = current->audit_context;
  1389. if (!audit_enabled)
  1390. return 0;
  1391. if (likely(!context))
  1392. return 0;
  1393. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1394. if (!ax)
  1395. return -ENOMEM;
  1396. if (u_abs_timeout != NULL) {
  1397. if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
  1398. kfree(ax);
  1399. return -EFAULT;
  1400. }
  1401. } else
  1402. memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
  1403. ax->mqdes = mqdes;
  1404. ax->msg_len = msg_len;
  1405. ax->msg_prio = msg_prio;
  1406. ax->d.type = AUDIT_MQ_SENDRECV;
  1407. ax->d.next = context->aux;
  1408. context->aux = (void *)ax;
  1409. return 0;
  1410. }
  1411. /**
  1412. * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
  1413. * @mqdes: MQ descriptor
  1414. * @msg_len: Message length
  1415. * @u_msg_prio: Message priority
  1416. * @u_abs_timeout: Message timeout in absolute time
  1417. *
  1418. * Returns 0 for success or NULL context or < 0 on error.
  1419. */
  1420. int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
  1421. unsigned int __user *u_msg_prio,
  1422. const struct timespec __user *u_abs_timeout)
  1423. {
  1424. struct audit_aux_data_mq_sendrecv *ax;
  1425. struct audit_context *context = current->audit_context;
  1426. if (!audit_enabled)
  1427. return 0;
  1428. if (likely(!context))
  1429. return 0;
  1430. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1431. if (!ax)
  1432. return -ENOMEM;
  1433. if (u_msg_prio != NULL) {
  1434. if (get_user(ax->msg_prio, u_msg_prio)) {
  1435. kfree(ax);
  1436. return -EFAULT;
  1437. }
  1438. } else
  1439. ax->msg_prio = 0;
  1440. if (u_abs_timeout != NULL) {
  1441. if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
  1442. kfree(ax);
  1443. return -EFAULT;
  1444. }
  1445. } else
  1446. memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
  1447. ax->mqdes = mqdes;
  1448. ax->msg_len = msg_len;
  1449. ax->d.type = AUDIT_MQ_SENDRECV;
  1450. ax->d.next = context->aux;
  1451. context->aux = (void *)ax;
  1452. return 0;
  1453. }
  1454. /**
  1455. * __audit_mq_notify - record audit data for a POSIX MQ notify
  1456. * @mqdes: MQ descriptor
  1457. * @u_notification: Notification event
  1458. *
  1459. * Returns 0 for success or NULL context or < 0 on error.
  1460. */
  1461. int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
  1462. {
  1463. struct audit_aux_data_mq_notify *ax;
  1464. struct audit_context *context = current->audit_context;
  1465. if (!audit_enabled)
  1466. return 0;
  1467. if (likely(!context))
  1468. return 0;
  1469. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1470. if (!ax)
  1471. return -ENOMEM;
  1472. if (u_notification != NULL) {
  1473. if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
  1474. kfree(ax);
  1475. return -EFAULT;
  1476. }
  1477. } else
  1478. memset(&ax->notification, 0, sizeof(ax->notification));
  1479. ax->mqdes = mqdes;
  1480. ax->d.type = AUDIT_MQ_NOTIFY;
  1481. ax->d.next = context->aux;
  1482. context->aux = (void *)ax;
  1483. return 0;
  1484. }
  1485. /**
  1486. * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
  1487. * @mqdes: MQ descriptor
  1488. * @mqstat: MQ flags
  1489. *
  1490. * Returns 0 for success or NULL context or < 0 on error.
  1491. */
  1492. int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
  1493. {
  1494. struct audit_aux_data_mq_getsetattr *ax;
  1495. struct audit_context *context = current->audit_context;
  1496. if (!audit_enabled)
  1497. return 0;
  1498. if (likely(!context))
  1499. return 0;
  1500. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1501. if (!ax)
  1502. return -ENOMEM;
  1503. ax->mqdes = mqdes;
  1504. ax->mqstat = *mqstat;
  1505. ax->d.type = AUDIT_MQ_GETSETATTR;
  1506. ax->d.next = context->aux;
  1507. context->aux = (void *)ax;
  1508. return 0;
  1509. }
  1510. /**
  1511. * audit_ipc_obj - record audit data for ipc object
  1512. * @ipcp: ipc permissions
  1513. *
  1514. * Returns 0 for success or NULL context or < 0 on error.
  1515. */
  1516. int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
  1517. {
  1518. struct audit_aux_data_ipcctl *ax;
  1519. struct audit_context *context = current->audit_context;
  1520. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1521. if (!ax)
  1522. return -ENOMEM;
  1523. ax->uid = ipcp->uid;
  1524. ax->gid = ipcp->gid;
  1525. ax->mode = ipcp->mode;
  1526. selinux_get_ipc_sid(ipcp, &ax->osid);
  1527. ax->d.type = AUDIT_IPC;
  1528. ax->d.next = context->aux;
  1529. context->aux = (void *)ax;
  1530. return 0;
  1531. }
  1532. /**
  1533. * audit_ipc_set_perm - record audit data for new ipc permissions
  1534. * @qbytes: msgq bytes
  1535. * @uid: msgq user id
  1536. * @gid: msgq group id
  1537. * @mode: msgq mode (permissions)
  1538. *
  1539. * Returns 0 for success or NULL context or < 0 on error.
  1540. */
  1541. int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
  1542. {
  1543. struct audit_aux_data_ipcctl *ax;
  1544. struct audit_context *context = current->audit_context;
  1545. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1546. if (!ax)
  1547. return -ENOMEM;
  1548. ax->qbytes = qbytes;
  1549. ax->uid = uid;
  1550. ax->gid = gid;
  1551. ax->mode = mode;
  1552. ax->d.type = AUDIT_IPC_SET_PERM;
  1553. ax->d.next = context->aux;
  1554. context->aux = (void *)ax;
  1555. return 0;
  1556. }
  1557. int audit_bprm(struct linux_binprm *bprm)
  1558. {
  1559. struct audit_aux_data_execve *ax;
  1560. struct audit_context *context = current->audit_context;
  1561. unsigned long p, next;
  1562. void *to;
  1563. if (likely(!audit_enabled || !context || context->dummy))
  1564. return 0;
  1565. ax = kmalloc(sizeof(*ax) + PAGE_SIZE * MAX_ARG_PAGES - bprm->p,
  1566. GFP_KERNEL);
  1567. if (!ax)
  1568. return -ENOMEM;
  1569. ax->argc = bprm->argc;
  1570. ax->envc = bprm->envc;
  1571. for (p = bprm->p, to = ax->mem; p < MAX_ARG_PAGES*PAGE_SIZE; p = next) {
  1572. struct page *page = bprm->page[p / PAGE_SIZE];
  1573. void *kaddr = kmap(page);
  1574. next = (p + PAGE_SIZE) & ~(PAGE_SIZE - 1);
  1575. memcpy(to, kaddr + (p & (PAGE_SIZE - 1)), next - p);
  1576. to += next - p;
  1577. kunmap(page);
  1578. }
  1579. ax->d.type = AUDIT_EXECVE;
  1580. ax->d.next = context->aux;
  1581. context->aux = (void *)ax;
  1582. return 0;
  1583. }
  1584. /**
  1585. * audit_socketcall - record audit data for sys_socketcall
  1586. * @nargs: number of args
  1587. * @args: args array
  1588. *
  1589. * Returns 0 for success or NULL context or < 0 on error.
  1590. */
  1591. int audit_socketcall(int nargs, unsigned long *args)
  1592. {
  1593. struct audit_aux_data_socketcall *ax;
  1594. struct audit_context *context = current->audit_context;
  1595. if (likely(!context || context->dummy))
  1596. return 0;
  1597. ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
  1598. if (!ax)
  1599. return -ENOMEM;
  1600. ax->nargs = nargs;
  1601. memcpy(ax->args, args, nargs * sizeof(unsigned long));
  1602. ax->d.type = AUDIT_SOCKETCALL;
  1603. ax->d.next = context->aux;
  1604. context->aux = (void *)ax;
  1605. return 0;
  1606. }
  1607. /**
  1608. * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
  1609. * @len: data length in user space
  1610. * @a: data address in kernel space
  1611. *
  1612. * Returns 0 for success or NULL context or < 0 on error.
  1613. */
  1614. int audit_sockaddr(int len, void *a)
  1615. {
  1616. struct audit_aux_data_sockaddr *ax;
  1617. struct audit_context *context = current->audit_context;
  1618. if (likely(!context || context->dummy))
  1619. return 0;
  1620. ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
  1621. if (!ax)
  1622. return -ENOMEM;
  1623. ax->len = len;
  1624. memcpy(ax->a, a, len);
  1625. ax->d.type = AUDIT_SOCKADDR;
  1626. ax->d.next = context->aux;
  1627. context->aux = (void *)ax;
  1628. return 0;
  1629. }
  1630. /**
  1631. * audit_avc_path - record the granting or denial of permissions
  1632. * @dentry: dentry to record
  1633. * @mnt: mnt to record
  1634. *
  1635. * Returns 0 for success or NULL context or < 0 on error.
  1636. *
  1637. * Called from security/selinux/avc.c::avc_audit()
  1638. */
  1639. int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
  1640. {
  1641. struct audit_aux_data_path *ax;
  1642. struct audit_context *context = current->audit_context;
  1643. if (likely(!context))
  1644. return 0;
  1645. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1646. if (!ax)
  1647. return -ENOMEM;
  1648. ax->dentry = dget(dentry);
  1649. ax->mnt = mntget(mnt);
  1650. ax->d.type = AUDIT_AVC_PATH;
  1651. ax->d.next = context->aux;
  1652. context->aux = (void *)ax;
  1653. return 0;
  1654. }
  1655. /**
  1656. * audit_signal_info - record signal info for shutting down audit subsystem
  1657. * @sig: signal value
  1658. * @t: task being signaled
  1659. *
  1660. * If the audit subsystem is being terminated, record the task (pid)
  1661. * and uid that is doing that.
  1662. */
  1663. void __audit_signal_info(int sig, struct task_struct *t)
  1664. {
  1665. extern pid_t audit_sig_pid;
  1666. extern uid_t audit_sig_uid;
  1667. extern u32 audit_sig_sid;
  1668. if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
  1669. struct task_struct *tsk = current;
  1670. struct audit_context *ctx = tsk->audit_context;
  1671. audit_sig_pid = tsk->pid;
  1672. if (ctx)
  1673. audit_sig_uid = ctx->loginuid;
  1674. else
  1675. audit_sig_uid = tsk->uid;
  1676. selinux_get_task_sid(tsk, &audit_sig_sid);
  1677. }
  1678. }