auditsc.c 46 KB

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