auditsc.c 52 KB

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