auditfilter.c 34 KB

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