auditsc.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072
  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. /**
  1367. * auditsc_get_stamp - get local copies of audit_context values
  1368. * @ctx: audit_context for the task
  1369. * @t: timespec to store time recorded in the audit_context
  1370. * @serial: serial value that is recorded in the audit_context
  1371. *
  1372. * Also sets the context as auditable.
  1373. */
  1374. void auditsc_get_stamp(struct audit_context *ctx,
  1375. struct timespec *t, unsigned int *serial)
  1376. {
  1377. if (!ctx->serial)
  1378. ctx->serial = audit_serial();
  1379. t->tv_sec = ctx->ctime.tv_sec;
  1380. t->tv_nsec = ctx->ctime.tv_nsec;
  1381. *serial = ctx->serial;
  1382. ctx->auditable = 1;
  1383. }
  1384. /**
  1385. * audit_set_loginuid - set a task's audit_context loginuid
  1386. * @task: task whose audit context is being modified
  1387. * @loginuid: loginuid value
  1388. *
  1389. * Returns 0.
  1390. *
  1391. * Called (set) from fs/proc/base.c::proc_loginuid_write().
  1392. */
  1393. int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
  1394. {
  1395. struct audit_context *context = task->audit_context;
  1396. if (context) {
  1397. /* Only log if audit is enabled */
  1398. if (context->in_syscall) {
  1399. struct audit_buffer *ab;
  1400. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
  1401. if (ab) {
  1402. audit_log_format(ab, "login pid=%d uid=%u "
  1403. "old auid=%u new auid=%u",
  1404. task->pid, task->uid,
  1405. context->loginuid, loginuid);
  1406. audit_log_end(ab);
  1407. }
  1408. }
  1409. context->loginuid = loginuid;
  1410. }
  1411. return 0;
  1412. }
  1413. /**
  1414. * audit_get_loginuid - get the loginuid for an audit_context
  1415. * @ctx: the audit_context
  1416. *
  1417. * Returns the context's loginuid or -1 if @ctx is NULL.
  1418. */
  1419. uid_t audit_get_loginuid(struct audit_context *ctx)
  1420. {
  1421. return ctx ? ctx->loginuid : -1;
  1422. }
  1423. EXPORT_SYMBOL(audit_get_loginuid);
  1424. /**
  1425. * __audit_mq_open - record audit data for a POSIX MQ open
  1426. * @oflag: open flag
  1427. * @mode: mode bits
  1428. * @u_attr: queue attributes
  1429. *
  1430. * Returns 0 for success or NULL context or < 0 on error.
  1431. */
  1432. int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
  1433. {
  1434. struct audit_aux_data_mq_open *ax;
  1435. struct audit_context *context = current->audit_context;
  1436. if (!audit_enabled)
  1437. return 0;
  1438. if (likely(!context))
  1439. return 0;
  1440. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1441. if (!ax)
  1442. return -ENOMEM;
  1443. if (u_attr != NULL) {
  1444. if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
  1445. kfree(ax);
  1446. return -EFAULT;
  1447. }
  1448. } else
  1449. memset(&ax->attr, 0, sizeof(ax->attr));
  1450. ax->oflag = oflag;
  1451. ax->mode = mode;
  1452. ax->d.type = AUDIT_MQ_OPEN;
  1453. ax->d.next = context->aux;
  1454. context->aux = (void *)ax;
  1455. return 0;
  1456. }
  1457. /**
  1458. * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
  1459. * @mqdes: MQ descriptor
  1460. * @msg_len: Message length
  1461. * @msg_prio: Message priority
  1462. * @u_abs_timeout: Message timeout in absolute time
  1463. *
  1464. * Returns 0 for success or NULL context or < 0 on error.
  1465. */
  1466. int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
  1467. const struct timespec __user *u_abs_timeout)
  1468. {
  1469. struct audit_aux_data_mq_sendrecv *ax;
  1470. struct audit_context *context = current->audit_context;
  1471. if (!audit_enabled)
  1472. return 0;
  1473. if (likely(!context))
  1474. return 0;
  1475. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1476. if (!ax)
  1477. return -ENOMEM;
  1478. if (u_abs_timeout != NULL) {
  1479. if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
  1480. kfree(ax);
  1481. return -EFAULT;
  1482. }
  1483. } else
  1484. memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
  1485. ax->mqdes = mqdes;
  1486. ax->msg_len = msg_len;
  1487. ax->msg_prio = msg_prio;
  1488. ax->d.type = AUDIT_MQ_SENDRECV;
  1489. ax->d.next = context->aux;
  1490. context->aux = (void *)ax;
  1491. return 0;
  1492. }
  1493. /**
  1494. * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
  1495. * @mqdes: MQ descriptor
  1496. * @msg_len: Message length
  1497. * @u_msg_prio: Message priority
  1498. * @u_abs_timeout: Message timeout in absolute time
  1499. *
  1500. * Returns 0 for success or NULL context or < 0 on error.
  1501. */
  1502. int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
  1503. unsigned int __user *u_msg_prio,
  1504. const struct timespec __user *u_abs_timeout)
  1505. {
  1506. struct audit_aux_data_mq_sendrecv *ax;
  1507. struct audit_context *context = current->audit_context;
  1508. if (!audit_enabled)
  1509. return 0;
  1510. if (likely(!context))
  1511. return 0;
  1512. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1513. if (!ax)
  1514. return -ENOMEM;
  1515. if (u_msg_prio != NULL) {
  1516. if (get_user(ax->msg_prio, u_msg_prio)) {
  1517. kfree(ax);
  1518. return -EFAULT;
  1519. }
  1520. } else
  1521. ax->msg_prio = 0;
  1522. if (u_abs_timeout != NULL) {
  1523. if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
  1524. kfree(ax);
  1525. return -EFAULT;
  1526. }
  1527. } else
  1528. memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
  1529. ax->mqdes = mqdes;
  1530. ax->msg_len = msg_len;
  1531. ax->d.type = AUDIT_MQ_SENDRECV;
  1532. ax->d.next = context->aux;
  1533. context->aux = (void *)ax;
  1534. return 0;
  1535. }
  1536. /**
  1537. * __audit_mq_notify - record audit data for a POSIX MQ notify
  1538. * @mqdes: MQ descriptor
  1539. * @u_notification: Notification event
  1540. *
  1541. * Returns 0 for success or NULL context or < 0 on error.
  1542. */
  1543. int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
  1544. {
  1545. struct audit_aux_data_mq_notify *ax;
  1546. struct audit_context *context = current->audit_context;
  1547. if (!audit_enabled)
  1548. return 0;
  1549. if (likely(!context))
  1550. return 0;
  1551. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1552. if (!ax)
  1553. return -ENOMEM;
  1554. if (u_notification != NULL) {
  1555. if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
  1556. kfree(ax);
  1557. return -EFAULT;
  1558. }
  1559. } else
  1560. memset(&ax->notification, 0, sizeof(ax->notification));
  1561. ax->mqdes = mqdes;
  1562. ax->d.type = AUDIT_MQ_NOTIFY;
  1563. ax->d.next = context->aux;
  1564. context->aux = (void *)ax;
  1565. return 0;
  1566. }
  1567. /**
  1568. * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
  1569. * @mqdes: MQ descriptor
  1570. * @mqstat: MQ flags
  1571. *
  1572. * Returns 0 for success or NULL context or < 0 on error.
  1573. */
  1574. int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
  1575. {
  1576. struct audit_aux_data_mq_getsetattr *ax;
  1577. struct audit_context *context = current->audit_context;
  1578. if (!audit_enabled)
  1579. return 0;
  1580. if (likely(!context))
  1581. return 0;
  1582. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1583. if (!ax)
  1584. return -ENOMEM;
  1585. ax->mqdes = mqdes;
  1586. ax->mqstat = *mqstat;
  1587. ax->d.type = AUDIT_MQ_GETSETATTR;
  1588. ax->d.next = context->aux;
  1589. context->aux = (void *)ax;
  1590. return 0;
  1591. }
  1592. /**
  1593. * audit_ipc_obj - record audit data for ipc object
  1594. * @ipcp: ipc permissions
  1595. *
  1596. * Returns 0 for success or NULL context or < 0 on error.
  1597. */
  1598. int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
  1599. {
  1600. struct audit_aux_data_ipcctl *ax;
  1601. struct audit_context *context = current->audit_context;
  1602. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1603. if (!ax)
  1604. return -ENOMEM;
  1605. ax->uid = ipcp->uid;
  1606. ax->gid = ipcp->gid;
  1607. ax->mode = ipcp->mode;
  1608. selinux_get_ipc_sid(ipcp, &ax->osid);
  1609. ax->d.type = AUDIT_IPC;
  1610. ax->d.next = context->aux;
  1611. context->aux = (void *)ax;
  1612. return 0;
  1613. }
  1614. /**
  1615. * audit_ipc_set_perm - record audit data for new ipc permissions
  1616. * @qbytes: msgq bytes
  1617. * @uid: msgq user id
  1618. * @gid: msgq group id
  1619. * @mode: msgq mode (permissions)
  1620. *
  1621. * Returns 0 for success or NULL context or < 0 on error.
  1622. */
  1623. int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
  1624. {
  1625. struct audit_aux_data_ipcctl *ax;
  1626. struct audit_context *context = current->audit_context;
  1627. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1628. if (!ax)
  1629. return -ENOMEM;
  1630. ax->qbytes = qbytes;
  1631. ax->uid = uid;
  1632. ax->gid = gid;
  1633. ax->mode = mode;
  1634. ax->d.type = AUDIT_IPC_SET_PERM;
  1635. ax->d.next = context->aux;
  1636. context->aux = (void *)ax;
  1637. return 0;
  1638. }
  1639. int audit_argv_kb = 32;
  1640. int audit_bprm(struct linux_binprm *bprm)
  1641. {
  1642. struct audit_aux_data_execve *ax;
  1643. struct audit_context *context = current->audit_context;
  1644. if (likely(!audit_enabled || !context || context->dummy))
  1645. return 0;
  1646. /*
  1647. * Even though the stack code doesn't limit the arg+env size any more,
  1648. * the audit code requires that _all_ arguments be logged in a single
  1649. * netlink skb. Hence cap it :-(
  1650. */
  1651. if (bprm->argv_len > (audit_argv_kb << 10))
  1652. return -E2BIG;
  1653. ax = kmalloc(sizeof(*ax), GFP_KERNEL);
  1654. if (!ax)
  1655. return -ENOMEM;
  1656. ax->argc = bprm->argc;
  1657. ax->envc = bprm->envc;
  1658. ax->mm = bprm->mm;
  1659. ax->d.type = AUDIT_EXECVE;
  1660. ax->d.next = context->aux;
  1661. context->aux = (void *)ax;
  1662. return 0;
  1663. }
  1664. /**
  1665. * audit_socketcall - record audit data for sys_socketcall
  1666. * @nargs: number of args
  1667. * @args: args array
  1668. *
  1669. * Returns 0 for success or NULL context or < 0 on error.
  1670. */
  1671. int audit_socketcall(int nargs, unsigned long *args)
  1672. {
  1673. struct audit_aux_data_socketcall *ax;
  1674. struct audit_context *context = current->audit_context;
  1675. if (likely(!context || context->dummy))
  1676. return 0;
  1677. ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
  1678. if (!ax)
  1679. return -ENOMEM;
  1680. ax->nargs = nargs;
  1681. memcpy(ax->args, args, nargs * sizeof(unsigned long));
  1682. ax->d.type = AUDIT_SOCKETCALL;
  1683. ax->d.next = context->aux;
  1684. context->aux = (void *)ax;
  1685. return 0;
  1686. }
  1687. /**
  1688. * __audit_fd_pair - record audit data for pipe and socketpair
  1689. * @fd1: the first file descriptor
  1690. * @fd2: the second file descriptor
  1691. *
  1692. * Returns 0 for success or NULL context or < 0 on error.
  1693. */
  1694. int __audit_fd_pair(int fd1, int fd2)
  1695. {
  1696. struct audit_context *context = current->audit_context;
  1697. struct audit_aux_data_fd_pair *ax;
  1698. if (likely(!context)) {
  1699. return 0;
  1700. }
  1701. ax = kmalloc(sizeof(*ax), GFP_KERNEL);
  1702. if (!ax) {
  1703. return -ENOMEM;
  1704. }
  1705. ax->fd[0] = fd1;
  1706. ax->fd[1] = fd2;
  1707. ax->d.type = AUDIT_FD_PAIR;
  1708. ax->d.next = context->aux;
  1709. context->aux = (void *)ax;
  1710. return 0;
  1711. }
  1712. /**
  1713. * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
  1714. * @len: data length in user space
  1715. * @a: data address in kernel space
  1716. *
  1717. * Returns 0 for success or NULL context or < 0 on error.
  1718. */
  1719. int audit_sockaddr(int len, void *a)
  1720. {
  1721. struct audit_aux_data_sockaddr *ax;
  1722. struct audit_context *context = current->audit_context;
  1723. if (likely(!context || context->dummy))
  1724. return 0;
  1725. ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
  1726. if (!ax)
  1727. return -ENOMEM;
  1728. ax->len = len;
  1729. memcpy(ax->a, a, len);
  1730. ax->d.type = AUDIT_SOCKADDR;
  1731. ax->d.next = context->aux;
  1732. context->aux = (void *)ax;
  1733. return 0;
  1734. }
  1735. void __audit_ptrace(struct task_struct *t)
  1736. {
  1737. struct audit_context *context = current->audit_context;
  1738. context->target_pid = t->pid;
  1739. selinux_get_task_sid(t, &context->target_sid);
  1740. }
  1741. /**
  1742. * audit_signal_info - record signal info for shutting down audit subsystem
  1743. * @sig: signal value
  1744. * @t: task being signaled
  1745. *
  1746. * If the audit subsystem is being terminated, record the task (pid)
  1747. * and uid that is doing that.
  1748. */
  1749. int __audit_signal_info(int sig, struct task_struct *t)
  1750. {
  1751. struct audit_aux_data_pids *axp;
  1752. struct task_struct *tsk = current;
  1753. struct audit_context *ctx = tsk->audit_context;
  1754. extern pid_t audit_sig_pid;
  1755. extern uid_t audit_sig_uid;
  1756. extern u32 audit_sig_sid;
  1757. if (audit_pid && t->tgid == audit_pid) {
  1758. if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
  1759. audit_sig_pid = tsk->pid;
  1760. if (ctx)
  1761. audit_sig_uid = ctx->loginuid;
  1762. else
  1763. audit_sig_uid = tsk->uid;
  1764. selinux_get_task_sid(tsk, &audit_sig_sid);
  1765. }
  1766. if (!audit_signals || audit_dummy_context())
  1767. return 0;
  1768. }
  1769. /* optimize the common case by putting first signal recipient directly
  1770. * in audit_context */
  1771. if (!ctx->target_pid) {
  1772. ctx->target_pid = t->tgid;
  1773. selinux_get_task_sid(t, &ctx->target_sid);
  1774. return 0;
  1775. }
  1776. axp = (void *)ctx->aux_pids;
  1777. if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
  1778. axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
  1779. if (!axp)
  1780. return -ENOMEM;
  1781. axp->d.type = AUDIT_OBJ_PID;
  1782. axp->d.next = ctx->aux_pids;
  1783. ctx->aux_pids = (void *)axp;
  1784. }
  1785. BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
  1786. axp->target_pid[axp->pid_count] = t->tgid;
  1787. selinux_get_task_sid(t, &axp->target_sid[axp->pid_count]);
  1788. axp->pid_count++;
  1789. return 0;
  1790. }
  1791. /**
  1792. * audit_core_dumps - record information about processes that end abnormally
  1793. * @signr: signal value
  1794. *
  1795. * If a process ends with a core dump, something fishy is going on and we
  1796. * should record the event for investigation.
  1797. */
  1798. void audit_core_dumps(long signr)
  1799. {
  1800. struct audit_buffer *ab;
  1801. u32 sid;
  1802. if (!audit_enabled)
  1803. return;
  1804. if (signr == SIGQUIT) /* don't care for those */
  1805. return;
  1806. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
  1807. audit_log_format(ab, "auid=%u uid=%u gid=%u",
  1808. audit_get_loginuid(current->audit_context),
  1809. current->uid, current->gid);
  1810. selinux_get_task_sid(current, &sid);
  1811. if (sid) {
  1812. char *ctx = NULL;
  1813. u32 len;
  1814. if (selinux_sid_to_string(sid, &ctx, &len))
  1815. audit_log_format(ab, " ssid=%u", sid);
  1816. else
  1817. audit_log_format(ab, " subj=%s", ctx);
  1818. kfree(ctx);
  1819. }
  1820. audit_log_format(ab, " pid=%d comm=", current->pid);
  1821. audit_log_untrustedstring(ab, current->comm);
  1822. audit_log_format(ab, " sig=%ld", signr);
  1823. audit_log_end(ab);
  1824. }