auditfilter.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523
  1. /* auditfilter.c -- filtering of audit events
  2. *
  3. * Copyright 2003-2004 Red Hat, Inc.
  4. * Copyright 2005 Hewlett-Packard Development Company, L.P.
  5. * Copyright 2005 IBM Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/audit.h>
  23. #include <linux/kthread.h>
  24. #include <linux/mutex.h>
  25. #include <linux/fs.h>
  26. #include <linux/namei.h>
  27. #include <linux/netlink.h>
  28. #include <linux/sched.h>
  29. #include <linux/slab.h>
  30. #include <linux/security.h>
  31. #include "audit.h"
  32. /*
  33. * Locking model:
  34. *
  35. * audit_filter_mutex:
  36. * Synchronizes writes and blocking reads of audit's filterlist
  37. * data. Rcu is used to traverse the filterlist and access
  38. * contents of structs audit_entry, audit_watch and opaque
  39. * LSM rules during filtering. If modified, these structures
  40. * must be copied and replace their counterparts in the filterlist.
  41. * An audit_parent struct is not accessed during filtering, so may
  42. * be written directly provided audit_filter_mutex is held.
  43. */
  44. /* Audit filter lists, defined in <linux/audit.h> */
  45. struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
  46. LIST_HEAD_INIT(audit_filter_list[0]),
  47. LIST_HEAD_INIT(audit_filter_list[1]),
  48. LIST_HEAD_INIT(audit_filter_list[2]),
  49. LIST_HEAD_INIT(audit_filter_list[3]),
  50. LIST_HEAD_INIT(audit_filter_list[4]),
  51. LIST_HEAD_INIT(audit_filter_list[5]),
  52. #if AUDIT_NR_FILTERS != 6
  53. #error Fix audit_filter_list initialiser
  54. #endif
  55. };
  56. static struct list_head audit_rules_list[AUDIT_NR_FILTERS] = {
  57. LIST_HEAD_INIT(audit_rules_list[0]),
  58. LIST_HEAD_INIT(audit_rules_list[1]),
  59. LIST_HEAD_INIT(audit_rules_list[2]),
  60. LIST_HEAD_INIT(audit_rules_list[3]),
  61. LIST_HEAD_INIT(audit_rules_list[4]),
  62. LIST_HEAD_INIT(audit_rules_list[5]),
  63. };
  64. DEFINE_MUTEX(audit_filter_mutex);
  65. static inline void audit_free_rule(struct audit_entry *e)
  66. {
  67. int i;
  68. struct audit_krule *erule = &e->rule;
  69. /* some rules don't have associated watches */
  70. if (erule->watch)
  71. audit_put_watch(erule->watch);
  72. if (erule->fields)
  73. for (i = 0; i < erule->field_count; i++) {
  74. struct audit_field *f = &erule->fields[i];
  75. kfree(f->lsm_str);
  76. security_audit_rule_free(f->lsm_rule);
  77. }
  78. kfree(erule->fields);
  79. kfree(erule->filterkey);
  80. kfree(e);
  81. }
  82. void audit_free_rule_rcu(struct rcu_head *head)
  83. {
  84. struct audit_entry *e = container_of(head, struct audit_entry, rcu);
  85. audit_free_rule(e);
  86. }
  87. /* Initialize an audit filterlist entry. */
  88. static inline struct audit_entry *audit_init_entry(u32 field_count)
  89. {
  90. struct audit_entry *entry;
  91. struct audit_field *fields;
  92. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  93. if (unlikely(!entry))
  94. return NULL;
  95. fields = kzalloc(sizeof(*fields) * field_count, GFP_KERNEL);
  96. if (unlikely(!fields)) {
  97. kfree(entry);
  98. return NULL;
  99. }
  100. entry->rule.fields = fields;
  101. return entry;
  102. }
  103. /* Unpack a filter field's string representation from user-space
  104. * buffer. */
  105. char *audit_unpack_string(void **bufp, size_t *remain, size_t len)
  106. {
  107. char *str;
  108. if (!*bufp || (len == 0) || (len > *remain))
  109. return ERR_PTR(-EINVAL);
  110. /* Of the currently implemented string fields, PATH_MAX
  111. * defines the longest valid length.
  112. */
  113. if (len > PATH_MAX)
  114. return ERR_PTR(-ENAMETOOLONG);
  115. str = kmalloc(len + 1, GFP_KERNEL);
  116. if (unlikely(!str))
  117. return ERR_PTR(-ENOMEM);
  118. memcpy(str, *bufp, len);
  119. str[len] = 0;
  120. *bufp += len;
  121. *remain -= len;
  122. return str;
  123. }
  124. /* Translate an inode field to kernel respresentation. */
  125. static inline int audit_to_inode(struct audit_krule *krule,
  126. struct audit_field *f)
  127. {
  128. if (krule->listnr != AUDIT_FILTER_EXIT ||
  129. krule->watch || krule->inode_f || krule->tree ||
  130. (f->op != Audit_equal && f->op != Audit_not_equal))
  131. return -EINVAL;
  132. krule->inode_f = f;
  133. return 0;
  134. }
  135. static __u32 *classes[AUDIT_SYSCALL_CLASSES];
  136. int __init audit_register_class(int class, unsigned *list)
  137. {
  138. __u32 *p = kzalloc(AUDIT_BITMASK_SIZE * sizeof(__u32), GFP_KERNEL);
  139. if (!p)
  140. return -ENOMEM;
  141. while (*list != ~0U) {
  142. unsigned n = *list++;
  143. if (n >= AUDIT_BITMASK_SIZE * 32 - AUDIT_SYSCALL_CLASSES) {
  144. kfree(p);
  145. return -EINVAL;
  146. }
  147. p[AUDIT_WORD(n)] |= AUDIT_BIT(n);
  148. }
  149. if (class >= AUDIT_SYSCALL_CLASSES || classes[class]) {
  150. kfree(p);
  151. return -EINVAL;
  152. }
  153. classes[class] = p;
  154. return 0;
  155. }
  156. int audit_match_class(int class, unsigned syscall)
  157. {
  158. if (unlikely(syscall >= AUDIT_BITMASK_SIZE * 32))
  159. return 0;
  160. if (unlikely(class >= AUDIT_SYSCALL_CLASSES || !classes[class]))
  161. return 0;
  162. return classes[class][AUDIT_WORD(syscall)] & AUDIT_BIT(syscall);
  163. }
  164. #ifdef CONFIG_AUDITSYSCALL
  165. static inline int audit_match_class_bits(int class, u32 *mask)
  166. {
  167. int i;
  168. if (classes[class]) {
  169. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  170. if (mask[i] & classes[class][i])
  171. return 0;
  172. }
  173. return 1;
  174. }
  175. static int audit_match_signal(struct audit_entry *entry)
  176. {
  177. struct audit_field *arch = entry->rule.arch_f;
  178. if (!arch) {
  179. /* When arch is unspecified, we must check both masks on biarch
  180. * as syscall number alone is ambiguous. */
  181. return (audit_match_class_bits(AUDIT_CLASS_SIGNAL,
  182. entry->rule.mask) &&
  183. audit_match_class_bits(AUDIT_CLASS_SIGNAL_32,
  184. entry->rule.mask));
  185. }
  186. switch(audit_classify_arch(arch->val)) {
  187. case 0: /* native */
  188. return (audit_match_class_bits(AUDIT_CLASS_SIGNAL,
  189. entry->rule.mask));
  190. case 1: /* 32bit on biarch */
  191. return (audit_match_class_bits(AUDIT_CLASS_SIGNAL_32,
  192. entry->rule.mask));
  193. default:
  194. return 1;
  195. }
  196. }
  197. #endif
  198. /* Common user-space to kernel rule translation. */
  199. static inline struct audit_entry *audit_to_entry_common(struct audit_rule *rule)
  200. {
  201. unsigned listnr;
  202. struct audit_entry *entry;
  203. int i, err;
  204. err = -EINVAL;
  205. listnr = rule->flags & ~AUDIT_FILTER_PREPEND;
  206. switch(listnr) {
  207. default:
  208. goto exit_err;
  209. #ifdef CONFIG_AUDITSYSCALL
  210. case AUDIT_FILTER_ENTRY:
  211. if (rule->action == AUDIT_ALWAYS)
  212. goto exit_err;
  213. case AUDIT_FILTER_EXIT:
  214. case AUDIT_FILTER_TASK:
  215. #endif
  216. case AUDIT_FILTER_USER:
  217. case AUDIT_FILTER_TYPE:
  218. ;
  219. }
  220. if (unlikely(rule->action == AUDIT_POSSIBLE)) {
  221. printk(KERN_ERR "AUDIT_POSSIBLE is deprecated\n");
  222. goto exit_err;
  223. }
  224. if (rule->action != AUDIT_NEVER && rule->action != AUDIT_ALWAYS)
  225. goto exit_err;
  226. if (rule->field_count > AUDIT_MAX_FIELDS)
  227. goto exit_err;
  228. err = -ENOMEM;
  229. entry = audit_init_entry(rule->field_count);
  230. if (!entry)
  231. goto exit_err;
  232. entry->rule.flags = rule->flags & AUDIT_FILTER_PREPEND;
  233. entry->rule.listnr = listnr;
  234. entry->rule.action = rule->action;
  235. entry->rule.field_count = rule->field_count;
  236. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  237. entry->rule.mask[i] = rule->mask[i];
  238. for (i = 0; i < AUDIT_SYSCALL_CLASSES; i++) {
  239. int bit = AUDIT_BITMASK_SIZE * 32 - i - 1;
  240. __u32 *p = &entry->rule.mask[AUDIT_WORD(bit)];
  241. __u32 *class;
  242. if (!(*p & AUDIT_BIT(bit)))
  243. continue;
  244. *p &= ~AUDIT_BIT(bit);
  245. class = classes[i];
  246. if (class) {
  247. int j;
  248. for (j = 0; j < AUDIT_BITMASK_SIZE; j++)
  249. entry->rule.mask[j] |= class[j];
  250. }
  251. }
  252. return entry;
  253. exit_err:
  254. return ERR_PTR(err);
  255. }
  256. static u32 audit_ops[] =
  257. {
  258. [Audit_equal] = AUDIT_EQUAL,
  259. [Audit_not_equal] = AUDIT_NOT_EQUAL,
  260. [Audit_bitmask] = AUDIT_BIT_MASK,
  261. [Audit_bittest] = AUDIT_BIT_TEST,
  262. [Audit_lt] = AUDIT_LESS_THAN,
  263. [Audit_gt] = AUDIT_GREATER_THAN,
  264. [Audit_le] = AUDIT_LESS_THAN_OR_EQUAL,
  265. [Audit_ge] = AUDIT_GREATER_THAN_OR_EQUAL,
  266. };
  267. static u32 audit_to_op(u32 op)
  268. {
  269. u32 n;
  270. for (n = Audit_equal; n < Audit_bad && audit_ops[n] != op; n++)
  271. ;
  272. return n;
  273. }
  274. /* check if an audit field is valid */
  275. static int audit_field_valid(struct audit_entry *entry, struct audit_field *f)
  276. {
  277. switch(f->type) {
  278. case AUDIT_MSGTYPE:
  279. if (entry->rule.listnr != AUDIT_FILTER_TYPE &&
  280. entry->rule.listnr != AUDIT_FILTER_USER)
  281. return -EINVAL;
  282. break;
  283. };
  284. switch(f->type) {
  285. default:
  286. return -EINVAL;
  287. case AUDIT_UID:
  288. case AUDIT_EUID:
  289. case AUDIT_SUID:
  290. case AUDIT_FSUID:
  291. case AUDIT_LOGINUID:
  292. case AUDIT_OBJ_UID:
  293. case AUDIT_GID:
  294. case AUDIT_EGID:
  295. case AUDIT_SGID:
  296. case AUDIT_FSGID:
  297. case AUDIT_OBJ_GID:
  298. case AUDIT_PID:
  299. case AUDIT_PERS:
  300. case AUDIT_MSGTYPE:
  301. case AUDIT_PPID:
  302. case AUDIT_DEVMAJOR:
  303. case AUDIT_DEVMINOR:
  304. case AUDIT_EXIT:
  305. case AUDIT_SUCCESS:
  306. /* bit ops are only useful on syscall args */
  307. if (f->op == Audit_bitmask || f->op == Audit_bittest)
  308. return -EINVAL;
  309. break;
  310. case AUDIT_ARG0:
  311. case AUDIT_ARG1:
  312. case AUDIT_ARG2:
  313. case AUDIT_ARG3:
  314. case AUDIT_SUBJ_USER:
  315. case AUDIT_SUBJ_ROLE:
  316. case AUDIT_SUBJ_TYPE:
  317. case AUDIT_SUBJ_SEN:
  318. case AUDIT_SUBJ_CLR:
  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. case AUDIT_WATCH:
  325. case AUDIT_DIR:
  326. case AUDIT_FILTERKEY:
  327. break;
  328. /* arch is only allowed to be = or != */
  329. case AUDIT_ARCH:
  330. if (f->op != Audit_not_equal && f->op != Audit_equal)
  331. return -EINVAL;
  332. break;
  333. case AUDIT_PERM:
  334. if (f->val & ~15)
  335. return -EINVAL;
  336. break;
  337. case AUDIT_FILETYPE:
  338. if (f->val & ~S_IFMT)
  339. return -EINVAL;
  340. break;
  341. case AUDIT_FIELD_COMPARE:
  342. if (f->val > AUDIT_MAX_FIELD_COMPARE)
  343. return -EINVAL;
  344. break;
  345. };
  346. return 0;
  347. }
  348. /* Translate struct audit_rule to kernel's rule respresentation.
  349. * Exists for backward compatibility with userspace. */
  350. static struct audit_entry *audit_rule_to_entry(struct audit_rule *rule)
  351. {
  352. struct audit_entry *entry;
  353. int err = 0;
  354. int i;
  355. entry = audit_to_entry_common(rule);
  356. if (IS_ERR(entry))
  357. goto exit_nofree;
  358. for (i = 0; i < rule->field_count; i++) {
  359. struct audit_field *f = &entry->rule.fields[i];
  360. u32 n;
  361. n = rule->fields[i] & (AUDIT_NEGATE|AUDIT_OPERATORS);
  362. /* Support for legacy operators where
  363. * AUDIT_NEGATE bit signifies != and otherwise assumes == */
  364. if (n & AUDIT_NEGATE)
  365. f->op = Audit_not_equal;
  366. else if (!n)
  367. f->op = Audit_equal;
  368. else
  369. f->op = audit_to_op(n);
  370. entry->rule.vers_ops = (n & AUDIT_OPERATORS) ? 2 : 1;
  371. f->type = rule->fields[i] & ~(AUDIT_NEGATE|AUDIT_OPERATORS);
  372. f->val = rule->values[i];
  373. f->uid = INVALID_UID;
  374. f->gid = INVALID_GID;
  375. err = -EINVAL;
  376. if (f->op == Audit_bad)
  377. goto exit_free;
  378. err = audit_field_valid(entry, f);
  379. if (err)
  380. goto exit_free;
  381. err = -EINVAL;
  382. switch (f->type) {
  383. case AUDIT_UID:
  384. case AUDIT_EUID:
  385. case AUDIT_SUID:
  386. case AUDIT_FSUID:
  387. case AUDIT_LOGINUID:
  388. f->uid = make_kuid(current_user_ns(), f->val);
  389. if (!uid_valid(f->uid))
  390. goto exit_free;
  391. break;
  392. case AUDIT_GID:
  393. case AUDIT_EGID:
  394. case AUDIT_SGID:
  395. case AUDIT_FSGID:
  396. f->gid = make_kgid(current_user_ns(), f->val);
  397. if (!gid_valid(f->gid))
  398. goto exit_free;
  399. break;
  400. case AUDIT_ARCH:
  401. entry->rule.arch_f = f;
  402. break;
  403. case AUDIT_INODE:
  404. err = audit_to_inode(&entry->rule, f);
  405. if (err)
  406. goto exit_free;
  407. break;
  408. }
  409. }
  410. if (entry->rule.inode_f && entry->rule.inode_f->op == Audit_not_equal)
  411. entry->rule.inode_f = NULL;
  412. exit_nofree:
  413. return entry;
  414. exit_free:
  415. audit_free_rule(entry);
  416. return ERR_PTR(err);
  417. }
  418. /* Translate struct audit_rule_data to kernel's rule respresentation. */
  419. static struct audit_entry *audit_data_to_entry(struct audit_rule_data *data,
  420. size_t datasz)
  421. {
  422. int err = 0;
  423. struct audit_entry *entry;
  424. void *bufp;
  425. size_t remain = datasz - sizeof(struct audit_rule_data);
  426. int i;
  427. char *str;
  428. entry = audit_to_entry_common((struct audit_rule *)data);
  429. if (IS_ERR(entry))
  430. goto exit_nofree;
  431. bufp = data->buf;
  432. entry->rule.vers_ops = 2;
  433. for (i = 0; i < data->field_count; i++) {
  434. struct audit_field *f = &entry->rule.fields[i];
  435. err = -EINVAL;
  436. f->op = audit_to_op(data->fieldflags[i]);
  437. if (f->op == Audit_bad)
  438. goto exit_free;
  439. f->type = data->fields[i];
  440. f->val = data->values[i];
  441. f->uid = INVALID_UID;
  442. f->gid = INVALID_GID;
  443. f->lsm_str = NULL;
  444. f->lsm_rule = NULL;
  445. err = audit_field_valid(entry, f);
  446. if (err)
  447. goto exit_free;
  448. err = -EINVAL;
  449. switch (f->type) {
  450. case AUDIT_UID:
  451. case AUDIT_EUID:
  452. case AUDIT_SUID:
  453. case AUDIT_FSUID:
  454. case AUDIT_LOGINUID:
  455. case AUDIT_OBJ_UID:
  456. f->uid = make_kuid(current_user_ns(), f->val);
  457. if (!uid_valid(f->uid))
  458. goto exit_free;
  459. break;
  460. case AUDIT_GID:
  461. case AUDIT_EGID:
  462. case AUDIT_SGID:
  463. case AUDIT_FSGID:
  464. case AUDIT_OBJ_GID:
  465. f->gid = make_kgid(current_user_ns(), f->val);
  466. if (!gid_valid(f->gid))
  467. goto exit_free;
  468. break;
  469. case AUDIT_ARCH:
  470. entry->rule.arch_f = f;
  471. break;
  472. case AUDIT_SUBJ_USER:
  473. case AUDIT_SUBJ_ROLE:
  474. case AUDIT_SUBJ_TYPE:
  475. case AUDIT_SUBJ_SEN:
  476. case AUDIT_SUBJ_CLR:
  477. case AUDIT_OBJ_USER:
  478. case AUDIT_OBJ_ROLE:
  479. case AUDIT_OBJ_TYPE:
  480. case AUDIT_OBJ_LEV_LOW:
  481. case AUDIT_OBJ_LEV_HIGH:
  482. str = audit_unpack_string(&bufp, &remain, f->val);
  483. if (IS_ERR(str))
  484. goto exit_free;
  485. entry->rule.buflen += f->val;
  486. err = security_audit_rule_init(f->type, f->op, str,
  487. (void **)&f->lsm_rule);
  488. /* Keep currently invalid fields around in case they
  489. * become valid after a policy reload. */
  490. if (err == -EINVAL) {
  491. printk(KERN_WARNING "audit rule for LSM "
  492. "\'%s\' is invalid\n", str);
  493. err = 0;
  494. }
  495. if (err) {
  496. kfree(str);
  497. goto exit_free;
  498. } else
  499. f->lsm_str = str;
  500. break;
  501. case AUDIT_WATCH:
  502. str = audit_unpack_string(&bufp, &remain, f->val);
  503. if (IS_ERR(str))
  504. goto exit_free;
  505. entry->rule.buflen += f->val;
  506. err = audit_to_watch(&entry->rule, str, f->val, f->op);
  507. if (err) {
  508. kfree(str);
  509. goto exit_free;
  510. }
  511. break;
  512. case AUDIT_DIR:
  513. str = audit_unpack_string(&bufp, &remain, f->val);
  514. if (IS_ERR(str))
  515. goto exit_free;
  516. entry->rule.buflen += f->val;
  517. err = audit_make_tree(&entry->rule, str, f->op);
  518. kfree(str);
  519. if (err)
  520. goto exit_free;
  521. break;
  522. case AUDIT_INODE:
  523. err = audit_to_inode(&entry->rule, f);
  524. if (err)
  525. goto exit_free;
  526. break;
  527. case AUDIT_FILTERKEY:
  528. if (entry->rule.filterkey || f->val > AUDIT_MAX_KEY_LEN)
  529. goto exit_free;
  530. str = audit_unpack_string(&bufp, &remain, f->val);
  531. if (IS_ERR(str))
  532. goto exit_free;
  533. entry->rule.buflen += f->val;
  534. entry->rule.filterkey = str;
  535. break;
  536. }
  537. }
  538. if (entry->rule.inode_f && entry->rule.inode_f->op == Audit_not_equal)
  539. entry->rule.inode_f = NULL;
  540. exit_nofree:
  541. return entry;
  542. exit_free:
  543. audit_free_rule(entry);
  544. return ERR_PTR(err);
  545. }
  546. /* Pack a filter field's string representation into data block. */
  547. static inline size_t audit_pack_string(void **bufp, const char *str)
  548. {
  549. size_t len = strlen(str);
  550. memcpy(*bufp, str, len);
  551. *bufp += len;
  552. return len;
  553. }
  554. /* Translate kernel rule respresentation to struct audit_rule.
  555. * Exists for backward compatibility with userspace. */
  556. static struct audit_rule *audit_krule_to_rule(struct audit_krule *krule)
  557. {
  558. struct audit_rule *rule;
  559. int i;
  560. rule = kzalloc(sizeof(*rule), GFP_KERNEL);
  561. if (unlikely(!rule))
  562. return NULL;
  563. rule->flags = krule->flags | krule->listnr;
  564. rule->action = krule->action;
  565. rule->field_count = krule->field_count;
  566. for (i = 0; i < rule->field_count; i++) {
  567. rule->values[i] = krule->fields[i].val;
  568. rule->fields[i] = krule->fields[i].type;
  569. if (krule->vers_ops == 1) {
  570. if (krule->fields[i].op == Audit_not_equal)
  571. rule->fields[i] |= AUDIT_NEGATE;
  572. } else {
  573. rule->fields[i] |= audit_ops[krule->fields[i].op];
  574. }
  575. }
  576. for (i = 0; i < AUDIT_BITMASK_SIZE; i++) rule->mask[i] = krule->mask[i];
  577. return rule;
  578. }
  579. /* Translate kernel rule respresentation to struct audit_rule_data. */
  580. static struct audit_rule_data *audit_krule_to_data(struct audit_krule *krule)
  581. {
  582. struct audit_rule_data *data;
  583. void *bufp;
  584. int i;
  585. data = kmalloc(sizeof(*data) + krule->buflen, GFP_KERNEL);
  586. if (unlikely(!data))
  587. return NULL;
  588. memset(data, 0, sizeof(*data));
  589. data->flags = krule->flags | krule->listnr;
  590. data->action = krule->action;
  591. data->field_count = krule->field_count;
  592. bufp = data->buf;
  593. for (i = 0; i < data->field_count; i++) {
  594. struct audit_field *f = &krule->fields[i];
  595. data->fields[i] = f->type;
  596. data->fieldflags[i] = audit_ops[f->op];
  597. switch(f->type) {
  598. case AUDIT_SUBJ_USER:
  599. case AUDIT_SUBJ_ROLE:
  600. case AUDIT_SUBJ_TYPE:
  601. case AUDIT_SUBJ_SEN:
  602. case AUDIT_SUBJ_CLR:
  603. case AUDIT_OBJ_USER:
  604. case AUDIT_OBJ_ROLE:
  605. case AUDIT_OBJ_TYPE:
  606. case AUDIT_OBJ_LEV_LOW:
  607. case AUDIT_OBJ_LEV_HIGH:
  608. data->buflen += data->values[i] =
  609. audit_pack_string(&bufp, f->lsm_str);
  610. break;
  611. case AUDIT_WATCH:
  612. data->buflen += data->values[i] =
  613. audit_pack_string(&bufp,
  614. audit_watch_path(krule->watch));
  615. break;
  616. case AUDIT_DIR:
  617. data->buflen += data->values[i] =
  618. audit_pack_string(&bufp,
  619. audit_tree_path(krule->tree));
  620. break;
  621. case AUDIT_FILTERKEY:
  622. data->buflen += data->values[i] =
  623. audit_pack_string(&bufp, krule->filterkey);
  624. break;
  625. default:
  626. data->values[i] = f->val;
  627. }
  628. }
  629. for (i = 0; i < AUDIT_BITMASK_SIZE; i++) data->mask[i] = krule->mask[i];
  630. return data;
  631. }
  632. /* Compare two rules in kernel format. Considered success if rules
  633. * don't match. */
  634. static int audit_compare_rule(struct audit_krule *a, struct audit_krule *b)
  635. {
  636. int i;
  637. if (a->flags != b->flags ||
  638. a->listnr != b->listnr ||
  639. a->action != b->action ||
  640. a->field_count != b->field_count)
  641. return 1;
  642. for (i = 0; i < a->field_count; i++) {
  643. if (a->fields[i].type != b->fields[i].type ||
  644. a->fields[i].op != b->fields[i].op)
  645. return 1;
  646. switch(a->fields[i].type) {
  647. case AUDIT_SUBJ_USER:
  648. case AUDIT_SUBJ_ROLE:
  649. case AUDIT_SUBJ_TYPE:
  650. case AUDIT_SUBJ_SEN:
  651. case AUDIT_SUBJ_CLR:
  652. case AUDIT_OBJ_USER:
  653. case AUDIT_OBJ_ROLE:
  654. case AUDIT_OBJ_TYPE:
  655. case AUDIT_OBJ_LEV_LOW:
  656. case AUDIT_OBJ_LEV_HIGH:
  657. if (strcmp(a->fields[i].lsm_str, b->fields[i].lsm_str))
  658. return 1;
  659. break;
  660. case AUDIT_WATCH:
  661. if (strcmp(audit_watch_path(a->watch),
  662. audit_watch_path(b->watch)))
  663. return 1;
  664. break;
  665. case AUDIT_DIR:
  666. if (strcmp(audit_tree_path(a->tree),
  667. audit_tree_path(b->tree)))
  668. return 1;
  669. break;
  670. case AUDIT_FILTERKEY:
  671. /* both filterkeys exist based on above type compare */
  672. if (strcmp(a->filterkey, b->filterkey))
  673. return 1;
  674. break;
  675. case AUDIT_UID:
  676. case AUDIT_EUID:
  677. case AUDIT_SUID:
  678. case AUDIT_FSUID:
  679. case AUDIT_LOGINUID:
  680. case AUDIT_OBJ_UID:
  681. if (!uid_eq(a->fields[i].uid, b->fields[i].uid))
  682. return 1;
  683. break;
  684. case AUDIT_GID:
  685. case AUDIT_EGID:
  686. case AUDIT_SGID:
  687. case AUDIT_FSGID:
  688. case AUDIT_OBJ_GID:
  689. if (!gid_eq(a->fields[i].gid, b->fields[i].gid))
  690. return 1;
  691. break;
  692. default:
  693. if (a->fields[i].val != b->fields[i].val)
  694. return 1;
  695. }
  696. }
  697. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  698. if (a->mask[i] != b->mask[i])
  699. return 1;
  700. return 0;
  701. }
  702. /* Duplicate LSM field information. The lsm_rule is opaque, so must be
  703. * re-initialized. */
  704. static inline int audit_dupe_lsm_field(struct audit_field *df,
  705. struct audit_field *sf)
  706. {
  707. int ret = 0;
  708. char *lsm_str;
  709. /* our own copy of lsm_str */
  710. lsm_str = kstrdup(sf->lsm_str, GFP_KERNEL);
  711. if (unlikely(!lsm_str))
  712. return -ENOMEM;
  713. df->lsm_str = lsm_str;
  714. /* our own (refreshed) copy of lsm_rule */
  715. ret = security_audit_rule_init(df->type, df->op, df->lsm_str,
  716. (void **)&df->lsm_rule);
  717. /* Keep currently invalid fields around in case they
  718. * become valid after a policy reload. */
  719. if (ret == -EINVAL) {
  720. printk(KERN_WARNING "audit rule for LSM \'%s\' is "
  721. "invalid\n", df->lsm_str);
  722. ret = 0;
  723. }
  724. return ret;
  725. }
  726. /* Duplicate an audit rule. This will be a deep copy with the exception
  727. * of the watch - that pointer is carried over. The LSM specific fields
  728. * will be updated in the copy. The point is to be able to replace the old
  729. * rule with the new rule in the filterlist, then free the old rule.
  730. * The rlist element is undefined; list manipulations are handled apart from
  731. * the initial copy. */
  732. struct audit_entry *audit_dupe_rule(struct audit_krule *old)
  733. {
  734. u32 fcount = old->field_count;
  735. struct audit_entry *entry;
  736. struct audit_krule *new;
  737. char *fk;
  738. int i, err = 0;
  739. entry = audit_init_entry(fcount);
  740. if (unlikely(!entry))
  741. return ERR_PTR(-ENOMEM);
  742. new = &entry->rule;
  743. new->vers_ops = old->vers_ops;
  744. new->flags = old->flags;
  745. new->listnr = old->listnr;
  746. new->action = old->action;
  747. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  748. new->mask[i] = old->mask[i];
  749. new->prio = old->prio;
  750. new->buflen = old->buflen;
  751. new->inode_f = old->inode_f;
  752. new->field_count = old->field_count;
  753. /*
  754. * note that we are OK with not refcounting here; audit_match_tree()
  755. * never dereferences tree and we can't get false positives there
  756. * since we'd have to have rule gone from the list *and* removed
  757. * before the chunks found by lookup had been allocated, i.e. before
  758. * the beginning of list scan.
  759. */
  760. new->tree = old->tree;
  761. memcpy(new->fields, old->fields, sizeof(struct audit_field) * fcount);
  762. /* deep copy this information, updating the lsm_rule fields, because
  763. * the originals will all be freed when the old rule is freed. */
  764. for (i = 0; i < fcount; i++) {
  765. switch (new->fields[i].type) {
  766. case AUDIT_SUBJ_USER:
  767. case AUDIT_SUBJ_ROLE:
  768. case AUDIT_SUBJ_TYPE:
  769. case AUDIT_SUBJ_SEN:
  770. case AUDIT_SUBJ_CLR:
  771. case AUDIT_OBJ_USER:
  772. case AUDIT_OBJ_ROLE:
  773. case AUDIT_OBJ_TYPE:
  774. case AUDIT_OBJ_LEV_LOW:
  775. case AUDIT_OBJ_LEV_HIGH:
  776. err = audit_dupe_lsm_field(&new->fields[i],
  777. &old->fields[i]);
  778. break;
  779. case AUDIT_FILTERKEY:
  780. fk = kstrdup(old->filterkey, GFP_KERNEL);
  781. if (unlikely(!fk))
  782. err = -ENOMEM;
  783. else
  784. new->filterkey = fk;
  785. }
  786. if (err) {
  787. audit_free_rule(entry);
  788. return ERR_PTR(err);
  789. }
  790. }
  791. if (old->watch) {
  792. audit_get_watch(old->watch);
  793. new->watch = old->watch;
  794. }
  795. return entry;
  796. }
  797. /* Find an existing audit rule.
  798. * Caller must hold audit_filter_mutex to prevent stale rule data. */
  799. static struct audit_entry *audit_find_rule(struct audit_entry *entry,
  800. struct list_head **p)
  801. {
  802. struct audit_entry *e, *found = NULL;
  803. struct list_head *list;
  804. int h;
  805. if (entry->rule.inode_f) {
  806. h = audit_hash_ino(entry->rule.inode_f->val);
  807. *p = list = &audit_inode_hash[h];
  808. } else if (entry->rule.watch) {
  809. /* we don't know the inode number, so must walk entire hash */
  810. for (h = 0; h < AUDIT_INODE_BUCKETS; h++) {
  811. list = &audit_inode_hash[h];
  812. list_for_each_entry(e, list, list)
  813. if (!audit_compare_rule(&entry->rule, &e->rule)) {
  814. found = e;
  815. goto out;
  816. }
  817. }
  818. goto out;
  819. } else {
  820. *p = list = &audit_filter_list[entry->rule.listnr];
  821. }
  822. list_for_each_entry(e, list, list)
  823. if (!audit_compare_rule(&entry->rule, &e->rule)) {
  824. found = e;
  825. goto out;
  826. }
  827. out:
  828. return found;
  829. }
  830. static u64 prio_low = ~0ULL/2;
  831. static u64 prio_high = ~0ULL/2 - 1;
  832. /* Add rule to given filterlist if not a duplicate. */
  833. static inline int audit_add_rule(struct audit_entry *entry)
  834. {
  835. struct audit_entry *e;
  836. struct audit_watch *watch = entry->rule.watch;
  837. struct audit_tree *tree = entry->rule.tree;
  838. struct list_head *list;
  839. int err;
  840. #ifdef CONFIG_AUDITSYSCALL
  841. int dont_count = 0;
  842. /* If either of these, don't count towards total */
  843. if (entry->rule.listnr == AUDIT_FILTER_USER ||
  844. entry->rule.listnr == AUDIT_FILTER_TYPE)
  845. dont_count = 1;
  846. #endif
  847. mutex_lock(&audit_filter_mutex);
  848. e = audit_find_rule(entry, &list);
  849. if (e) {
  850. mutex_unlock(&audit_filter_mutex);
  851. err = -EEXIST;
  852. /* normally audit_add_tree_rule() will free it on failure */
  853. if (tree)
  854. audit_put_tree(tree);
  855. goto error;
  856. }
  857. if (watch) {
  858. /* audit_filter_mutex is dropped and re-taken during this call */
  859. err = audit_add_watch(&entry->rule, &list);
  860. if (err) {
  861. mutex_unlock(&audit_filter_mutex);
  862. goto error;
  863. }
  864. }
  865. if (tree) {
  866. err = audit_add_tree_rule(&entry->rule);
  867. if (err) {
  868. mutex_unlock(&audit_filter_mutex);
  869. goto error;
  870. }
  871. }
  872. entry->rule.prio = ~0ULL;
  873. if (entry->rule.listnr == AUDIT_FILTER_EXIT) {
  874. if (entry->rule.flags & AUDIT_FILTER_PREPEND)
  875. entry->rule.prio = ++prio_high;
  876. else
  877. entry->rule.prio = --prio_low;
  878. }
  879. if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
  880. list_add(&entry->rule.list,
  881. &audit_rules_list[entry->rule.listnr]);
  882. list_add_rcu(&entry->list, list);
  883. entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
  884. } else {
  885. list_add_tail(&entry->rule.list,
  886. &audit_rules_list[entry->rule.listnr]);
  887. list_add_tail_rcu(&entry->list, list);
  888. }
  889. #ifdef CONFIG_AUDITSYSCALL
  890. if (!dont_count)
  891. audit_n_rules++;
  892. if (!audit_match_signal(entry))
  893. audit_signals++;
  894. #endif
  895. mutex_unlock(&audit_filter_mutex);
  896. return 0;
  897. error:
  898. if (watch)
  899. audit_put_watch(watch); /* tmp watch, matches initial get */
  900. return err;
  901. }
  902. /* Remove an existing rule from filterlist. */
  903. static inline int audit_del_rule(struct audit_entry *entry)
  904. {
  905. struct audit_entry *e;
  906. struct audit_watch *watch = entry->rule.watch;
  907. struct audit_tree *tree = entry->rule.tree;
  908. struct list_head *list;
  909. int ret = 0;
  910. #ifdef CONFIG_AUDITSYSCALL
  911. int dont_count = 0;
  912. /* If either of these, don't count towards total */
  913. if (entry->rule.listnr == AUDIT_FILTER_USER ||
  914. entry->rule.listnr == AUDIT_FILTER_TYPE)
  915. dont_count = 1;
  916. #endif
  917. mutex_lock(&audit_filter_mutex);
  918. e = audit_find_rule(entry, &list);
  919. if (!e) {
  920. mutex_unlock(&audit_filter_mutex);
  921. ret = -ENOENT;
  922. goto out;
  923. }
  924. if (e->rule.watch)
  925. audit_remove_watch_rule(&e->rule);
  926. if (e->rule.tree)
  927. audit_remove_tree_rule(&e->rule);
  928. list_del_rcu(&e->list);
  929. list_del(&e->rule.list);
  930. call_rcu(&e->rcu, audit_free_rule_rcu);
  931. #ifdef CONFIG_AUDITSYSCALL
  932. if (!dont_count)
  933. audit_n_rules--;
  934. if (!audit_match_signal(entry))
  935. audit_signals--;
  936. #endif
  937. mutex_unlock(&audit_filter_mutex);
  938. out:
  939. if (watch)
  940. audit_put_watch(watch); /* match initial get */
  941. if (tree)
  942. audit_put_tree(tree); /* that's the temporary one */
  943. return ret;
  944. }
  945. /* List rules using struct audit_rule. Exists for backward
  946. * compatibility with userspace. */
  947. static void audit_list(int pid, int seq, struct sk_buff_head *q)
  948. {
  949. struct sk_buff *skb;
  950. struct audit_krule *r;
  951. int i;
  952. /* This is a blocking read, so use audit_filter_mutex instead of rcu
  953. * iterator to sync with list writers. */
  954. for (i=0; i<AUDIT_NR_FILTERS; i++) {
  955. list_for_each_entry(r, &audit_rules_list[i], list) {
  956. struct audit_rule *rule;
  957. rule = audit_krule_to_rule(r);
  958. if (unlikely(!rule))
  959. break;
  960. skb = audit_make_reply(pid, seq, AUDIT_LIST, 0, 1,
  961. rule, sizeof(*rule));
  962. if (skb)
  963. skb_queue_tail(q, skb);
  964. kfree(rule);
  965. }
  966. }
  967. skb = audit_make_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
  968. if (skb)
  969. skb_queue_tail(q, skb);
  970. }
  971. /* List rules using struct audit_rule_data. */
  972. static void audit_list_rules(int pid, int seq, struct sk_buff_head *q)
  973. {
  974. struct sk_buff *skb;
  975. struct audit_krule *r;
  976. int i;
  977. /* This is a blocking read, so use audit_filter_mutex instead of rcu
  978. * iterator to sync with list writers. */
  979. for (i=0; i<AUDIT_NR_FILTERS; i++) {
  980. list_for_each_entry(r, &audit_rules_list[i], list) {
  981. struct audit_rule_data *data;
  982. data = audit_krule_to_data(r);
  983. if (unlikely(!data))
  984. break;
  985. skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 0, 1,
  986. data, sizeof(*data) + data->buflen);
  987. if (skb)
  988. skb_queue_tail(q, skb);
  989. kfree(data);
  990. }
  991. }
  992. skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 1, 1, NULL, 0);
  993. if (skb)
  994. skb_queue_tail(q, skb);
  995. }
  996. /* Log rule additions and removals */
  997. static void audit_log_rule_change(kuid_t loginuid, u32 sessionid, u32 sid,
  998. char *action, struct audit_krule *rule,
  999. int res)
  1000. {
  1001. struct audit_buffer *ab;
  1002. if (!audit_enabled)
  1003. return;
  1004. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
  1005. if (!ab)
  1006. return;
  1007. audit_log_format(ab, "auid=%u ses=%u",
  1008. from_kuid(&init_user_ns, loginuid), sessionid);
  1009. if (sid) {
  1010. char *ctx = NULL;
  1011. u32 len;
  1012. if (security_secid_to_secctx(sid, &ctx, &len))
  1013. audit_log_format(ab, " ssid=%u", sid);
  1014. else {
  1015. audit_log_format(ab, " subj=%s", ctx);
  1016. security_release_secctx(ctx, len);
  1017. }
  1018. }
  1019. audit_log_format(ab, " op=");
  1020. audit_log_string(ab, action);
  1021. audit_log_key(ab, rule->filterkey);
  1022. audit_log_format(ab, " list=%d res=%d", rule->listnr, res);
  1023. audit_log_end(ab);
  1024. }
  1025. /**
  1026. * audit_receive_filter - apply all rules to the specified message type
  1027. * @type: audit message type
  1028. * @pid: target pid for netlink audit messages
  1029. * @seq: netlink audit message sequence (serial) number
  1030. * @data: payload data
  1031. * @datasz: size of payload data
  1032. * @loginuid: loginuid of sender
  1033. * @sessionid: sessionid for netlink audit message
  1034. * @sid: SE Linux Security ID of sender
  1035. */
  1036. int audit_receive_filter(int type, int pid, int seq, void *data,
  1037. size_t datasz, kuid_t loginuid, u32 sessionid, u32 sid)
  1038. {
  1039. struct task_struct *tsk;
  1040. struct audit_netlink_list *dest;
  1041. int err = 0;
  1042. struct audit_entry *entry;
  1043. switch (type) {
  1044. case AUDIT_LIST:
  1045. case AUDIT_LIST_RULES:
  1046. /* We can't just spew out the rules here because we might fill
  1047. * the available socket buffer space and deadlock waiting for
  1048. * auditctl to read from it... which isn't ever going to
  1049. * happen if we're actually running in the context of auditctl
  1050. * trying to _send_ the stuff */
  1051. dest = kmalloc(sizeof(struct audit_netlink_list), GFP_KERNEL);
  1052. if (!dest)
  1053. return -ENOMEM;
  1054. dest->pid = pid;
  1055. skb_queue_head_init(&dest->q);
  1056. mutex_lock(&audit_filter_mutex);
  1057. if (type == AUDIT_LIST)
  1058. audit_list(pid, seq, &dest->q);
  1059. else
  1060. audit_list_rules(pid, seq, &dest->q);
  1061. mutex_unlock(&audit_filter_mutex);
  1062. tsk = kthread_run(audit_send_list, dest, "audit_send_list");
  1063. if (IS_ERR(tsk)) {
  1064. skb_queue_purge(&dest->q);
  1065. kfree(dest);
  1066. err = PTR_ERR(tsk);
  1067. }
  1068. break;
  1069. case AUDIT_ADD:
  1070. case AUDIT_ADD_RULE:
  1071. if (type == AUDIT_ADD)
  1072. entry = audit_rule_to_entry(data);
  1073. else
  1074. entry = audit_data_to_entry(data, datasz);
  1075. if (IS_ERR(entry))
  1076. return PTR_ERR(entry);
  1077. err = audit_add_rule(entry);
  1078. audit_log_rule_change(loginuid, sessionid, sid, "add rule",
  1079. &entry->rule, !err);
  1080. if (err)
  1081. audit_free_rule(entry);
  1082. break;
  1083. case AUDIT_DEL:
  1084. case AUDIT_DEL_RULE:
  1085. if (type == AUDIT_DEL)
  1086. entry = audit_rule_to_entry(data);
  1087. else
  1088. entry = audit_data_to_entry(data, datasz);
  1089. if (IS_ERR(entry))
  1090. return PTR_ERR(entry);
  1091. err = audit_del_rule(entry);
  1092. audit_log_rule_change(loginuid, sessionid, sid, "remove rule",
  1093. &entry->rule, !err);
  1094. audit_free_rule(entry);
  1095. break;
  1096. default:
  1097. return -EINVAL;
  1098. }
  1099. return err;
  1100. }
  1101. int audit_comparator(u32 left, u32 op, u32 right)
  1102. {
  1103. switch (op) {
  1104. case Audit_equal:
  1105. return (left == right);
  1106. case Audit_not_equal:
  1107. return (left != right);
  1108. case Audit_lt:
  1109. return (left < right);
  1110. case Audit_le:
  1111. return (left <= right);
  1112. case Audit_gt:
  1113. return (left > right);
  1114. case Audit_ge:
  1115. return (left >= right);
  1116. case Audit_bitmask:
  1117. return (left & right);
  1118. case Audit_bittest:
  1119. return ((left & right) == right);
  1120. default:
  1121. BUG();
  1122. return 0;
  1123. }
  1124. }
  1125. int audit_uid_comparator(kuid_t left, u32 op, kuid_t right)
  1126. {
  1127. switch (op) {
  1128. case Audit_equal:
  1129. return uid_eq(left, right);
  1130. case Audit_not_equal:
  1131. return !uid_eq(left, right);
  1132. case Audit_lt:
  1133. return uid_lt(left, right);
  1134. case Audit_le:
  1135. return uid_lte(left, right);
  1136. case Audit_gt:
  1137. return uid_gt(left, right);
  1138. case Audit_ge:
  1139. return uid_gte(left, right);
  1140. case Audit_bitmask:
  1141. case Audit_bittest:
  1142. default:
  1143. BUG();
  1144. return 0;
  1145. }
  1146. }
  1147. int audit_gid_comparator(kgid_t left, u32 op, kgid_t right)
  1148. {
  1149. switch (op) {
  1150. case Audit_equal:
  1151. return gid_eq(left, right);
  1152. case Audit_not_equal:
  1153. return !gid_eq(left, right);
  1154. case Audit_lt:
  1155. return gid_lt(left, right);
  1156. case Audit_le:
  1157. return gid_lte(left, right);
  1158. case Audit_gt:
  1159. return gid_gt(left, right);
  1160. case Audit_ge:
  1161. return gid_gte(left, right);
  1162. case Audit_bitmask:
  1163. case Audit_bittest:
  1164. default:
  1165. BUG();
  1166. return 0;
  1167. }
  1168. }
  1169. /**
  1170. * parent_len - find the length of the parent portion of a pathname
  1171. * @path: pathname of which to determine length
  1172. */
  1173. int parent_len(const char *path)
  1174. {
  1175. int plen;
  1176. const char *p;
  1177. plen = strlen(path);
  1178. if (plen == 0)
  1179. return plen;
  1180. /* disregard trailing slashes */
  1181. p = path + plen - 1;
  1182. while ((*p == '/') && (p > path))
  1183. p--;
  1184. /* walk backward until we find the next slash or hit beginning */
  1185. while ((*p != '/') && (p > path))
  1186. p--;
  1187. /* did we find a slash? Then increment to include it in path */
  1188. if (*p == '/')
  1189. p++;
  1190. return p - path;
  1191. }
  1192. /**
  1193. * audit_compare_dname_path - compare given dentry name with last component in
  1194. * given path. Return of 0 indicates a match.
  1195. * @dname: dentry name that we're comparing
  1196. * @path: full pathname that we're comparing
  1197. * @parentlen: length of the parent if known. Passing in AUDIT_NAME_FULL
  1198. * here indicates that we must compute this value.
  1199. */
  1200. int audit_compare_dname_path(const char *dname, const char *path, int parentlen)
  1201. {
  1202. int dlen, pathlen;
  1203. const char *p;
  1204. dlen = strlen(dname);
  1205. pathlen = strlen(path);
  1206. if (pathlen < dlen)
  1207. return 1;
  1208. parentlen = parentlen == AUDIT_NAME_FULL ? parent_len(path) : parentlen;
  1209. if (pathlen - parentlen != dlen)
  1210. return 1;
  1211. p = path + parentlen;
  1212. return strncmp(p, dname, dlen);
  1213. }
  1214. static int audit_filter_user_rules(struct audit_krule *rule, int type,
  1215. enum audit_state *state)
  1216. {
  1217. int i;
  1218. for (i = 0; i < rule->field_count; i++) {
  1219. struct audit_field *f = &rule->fields[i];
  1220. int result = 0;
  1221. u32 sid;
  1222. switch (f->type) {
  1223. case AUDIT_PID:
  1224. result = audit_comparator(task_pid_vnr(current), f->op, f->val);
  1225. break;
  1226. case AUDIT_UID:
  1227. result = audit_uid_comparator(current_uid(), f->op, f->uid);
  1228. break;
  1229. case AUDIT_GID:
  1230. result = audit_gid_comparator(current_gid(), f->op, f->gid);
  1231. break;
  1232. case AUDIT_LOGINUID:
  1233. result = audit_uid_comparator(audit_get_loginuid(current),
  1234. f->op, f->uid);
  1235. break;
  1236. case AUDIT_MSGTYPE:
  1237. result = audit_comparator(type, f->op, f->val);
  1238. break;
  1239. case AUDIT_SUBJ_USER:
  1240. case AUDIT_SUBJ_ROLE:
  1241. case AUDIT_SUBJ_TYPE:
  1242. case AUDIT_SUBJ_SEN:
  1243. case AUDIT_SUBJ_CLR:
  1244. if (f->lsm_rule) {
  1245. security_task_getsecid(current, &sid);
  1246. result = security_audit_rule_match(sid,
  1247. f->type,
  1248. f->op,
  1249. f->lsm_rule,
  1250. NULL);
  1251. }
  1252. break;
  1253. }
  1254. if (!result)
  1255. return 0;
  1256. }
  1257. switch (rule->action) {
  1258. case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
  1259. case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
  1260. }
  1261. return 1;
  1262. }
  1263. int audit_filter_user(int type)
  1264. {
  1265. enum audit_state state = AUDIT_DISABLED;
  1266. struct audit_entry *e;
  1267. int ret = 1;
  1268. rcu_read_lock();
  1269. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
  1270. if (audit_filter_user_rules(&e->rule, type, &state)) {
  1271. if (state == AUDIT_DISABLED)
  1272. ret = 0;
  1273. break;
  1274. }
  1275. }
  1276. rcu_read_unlock();
  1277. return ret; /* Audit by default */
  1278. }
  1279. int audit_filter_type(int type)
  1280. {
  1281. struct audit_entry *e;
  1282. int result = 0;
  1283. rcu_read_lock();
  1284. if (list_empty(&audit_filter_list[AUDIT_FILTER_TYPE]))
  1285. goto unlock_and_return;
  1286. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TYPE],
  1287. list) {
  1288. int i;
  1289. for (i = 0; i < e->rule.field_count; i++) {
  1290. struct audit_field *f = &e->rule.fields[i];
  1291. if (f->type == AUDIT_MSGTYPE) {
  1292. result = audit_comparator(type, f->op, f->val);
  1293. if (!result)
  1294. break;
  1295. }
  1296. }
  1297. if (result)
  1298. goto unlock_and_return;
  1299. }
  1300. unlock_and_return:
  1301. rcu_read_unlock();
  1302. return result;
  1303. }
  1304. static int update_lsm_rule(struct audit_krule *r)
  1305. {
  1306. struct audit_entry *entry = container_of(r, struct audit_entry, rule);
  1307. struct audit_entry *nentry;
  1308. int err = 0;
  1309. if (!security_audit_rule_known(r))
  1310. return 0;
  1311. nentry = audit_dupe_rule(r);
  1312. if (IS_ERR(nentry)) {
  1313. /* save the first error encountered for the
  1314. * return value */
  1315. err = PTR_ERR(nentry);
  1316. audit_panic("error updating LSM filters");
  1317. if (r->watch)
  1318. list_del(&r->rlist);
  1319. list_del_rcu(&entry->list);
  1320. list_del(&r->list);
  1321. } else {
  1322. if (r->watch || r->tree)
  1323. list_replace_init(&r->rlist, &nentry->rule.rlist);
  1324. list_replace_rcu(&entry->list, &nentry->list);
  1325. list_replace(&r->list, &nentry->rule.list);
  1326. }
  1327. call_rcu(&entry->rcu, audit_free_rule_rcu);
  1328. return err;
  1329. }
  1330. /* This function will re-initialize the lsm_rule field of all applicable rules.
  1331. * It will traverse the filter lists serarching for rules that contain LSM
  1332. * specific filter fields. When such a rule is found, it is copied, the
  1333. * LSM field is re-initialized, and the old rule is replaced with the
  1334. * updated rule. */
  1335. int audit_update_lsm_rules(void)
  1336. {
  1337. struct audit_krule *r, *n;
  1338. int i, err = 0;
  1339. /* audit_filter_mutex synchronizes the writers */
  1340. mutex_lock(&audit_filter_mutex);
  1341. for (i = 0; i < AUDIT_NR_FILTERS; i++) {
  1342. list_for_each_entry_safe(r, n, &audit_rules_list[i], list) {
  1343. int res = update_lsm_rule(r);
  1344. if (!err)
  1345. err = res;
  1346. }
  1347. }
  1348. mutex_unlock(&audit_filter_mutex);
  1349. return err;
  1350. }