auditsc.c 48 KB

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