lsm_audit.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * common LSM auditing functions
  4. *
  5. * Based on code written for SELinux by :
  6. * Stephen Smalley
  7. * James Morris <jmorris@redhat.com>
  8. * Author : Etienne Basset, <etienne.basset@ensta.org>
  9. */
  10. #include <linux/types.h>
  11. #include <linux/stddef.h>
  12. #include <linux/kernel.h>
  13. #include <linux/gfp.h>
  14. #include <linux/fs.h>
  15. #include <linux/init.h>
  16. #include <net/sock.h>
  17. #include <linux/un.h>
  18. #include <net/af_unix.h>
  19. #include <linux/audit.h>
  20. #include <linux/ipv6.h>
  21. #include <linux/ip.h>
  22. #include <net/ip.h>
  23. #include <net/ipv6.h>
  24. #include <linux/tcp.h>
  25. #include <linux/udp.h>
  26. #include <linux/sctp.h>
  27. #include <linux/lsm_audit.h>
  28. #include <linux/security.h>
  29. /**
  30. * ipv4_skb_to_auditdata : fill auditdata from skb
  31. * @skb : the skb
  32. * @ad : the audit data to fill
  33. * @proto : the layer 4 protocol
  34. *
  35. * return 0 on success
  36. */
  37. int ipv4_skb_to_auditdata(struct sk_buff *skb,
  38. struct common_audit_data *ad, u8 *proto)
  39. {
  40. int ret = 0;
  41. struct iphdr *ih;
  42. ih = ip_hdr(skb);
  43. ad->u.net->v4info.saddr = ih->saddr;
  44. ad->u.net->v4info.daddr = ih->daddr;
  45. if (proto)
  46. *proto = ih->protocol;
  47. /* non initial fragment */
  48. if (ntohs(ih->frag_off) & IP_OFFSET)
  49. return 0;
  50. switch (ih->protocol) {
  51. case IPPROTO_TCP: {
  52. struct tcphdr *th = tcp_hdr(skb);
  53. ad->u.net->sport = th->source;
  54. ad->u.net->dport = th->dest;
  55. break;
  56. }
  57. case IPPROTO_UDP: {
  58. struct udphdr *uh = udp_hdr(skb);
  59. ad->u.net->sport = uh->source;
  60. ad->u.net->dport = uh->dest;
  61. break;
  62. }
  63. case IPPROTO_SCTP: {
  64. struct sctphdr *sh = sctp_hdr(skb);
  65. ad->u.net->sport = sh->source;
  66. ad->u.net->dport = sh->dest;
  67. break;
  68. }
  69. default:
  70. ret = -EINVAL;
  71. }
  72. return ret;
  73. }
  74. #if IS_ENABLED(CONFIG_IPV6)
  75. /**
  76. * ipv6_skb_to_auditdata : fill auditdata from skb
  77. * @skb : the skb
  78. * @ad : the audit data to fill
  79. * @proto : the layer 4 protocol
  80. *
  81. * return 0 on success
  82. */
  83. int ipv6_skb_to_auditdata(struct sk_buff *skb,
  84. struct common_audit_data *ad, u8 *proto)
  85. {
  86. int offset, ret = 0;
  87. struct ipv6hdr *ip6;
  88. u8 nexthdr;
  89. __be16 frag_off;
  90. ip6 = ipv6_hdr(skb);
  91. ad->u.net->v6info.saddr = ip6->saddr;
  92. ad->u.net->v6info.daddr = ip6->daddr;
  93. /* IPv6 can have several extension header before the Transport header
  94. * skip them */
  95. offset = skb_network_offset(skb);
  96. offset += sizeof(*ip6);
  97. nexthdr = ip6->nexthdr;
  98. offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
  99. if (offset < 0)
  100. return 0;
  101. if (proto)
  102. *proto = nexthdr;
  103. switch (nexthdr) {
  104. case IPPROTO_TCP: {
  105. struct tcphdr _tcph, *th;
  106. th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
  107. if (th == NULL)
  108. break;
  109. ad->u.net->sport = th->source;
  110. ad->u.net->dport = th->dest;
  111. break;
  112. }
  113. case IPPROTO_UDP: {
  114. struct udphdr _udph, *uh;
  115. uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
  116. if (uh == NULL)
  117. break;
  118. ad->u.net->sport = uh->source;
  119. ad->u.net->dport = uh->dest;
  120. break;
  121. }
  122. case IPPROTO_SCTP: {
  123. struct sctphdr _sctph, *sh;
  124. sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
  125. if (sh == NULL)
  126. break;
  127. ad->u.net->sport = sh->source;
  128. ad->u.net->dport = sh->dest;
  129. break;
  130. }
  131. default:
  132. ret = -EINVAL;
  133. }
  134. return ret;
  135. }
  136. #endif
  137. static inline void print_ipv6_addr(struct audit_buffer *ab,
  138. const struct in6_addr *addr, __be16 port,
  139. const char *name1, const char *name2)
  140. {
  141. if (!ipv6_addr_any(addr))
  142. audit_log_format(ab, " %s=%pI6c", name1, addr);
  143. if (port)
  144. audit_log_format(ab, " %s=%d", name2, ntohs(port));
  145. }
  146. static inline void print_ipv4_addr(struct audit_buffer *ab, __be32 addr,
  147. __be16 port, const char *name1, const char *name2)
  148. {
  149. if (addr)
  150. audit_log_format(ab, " %s=%pI4", name1, &addr);
  151. if (port)
  152. audit_log_format(ab, " %s=%d", name2, ntohs(port));
  153. }
  154. /**
  155. * audit_log_lsm_data - helper to log common LSM audit data
  156. * @ab : the audit buffer
  157. * @a : common audit data
  158. */
  159. void audit_log_lsm_data(struct audit_buffer *ab,
  160. const struct common_audit_data *a)
  161. {
  162. /*
  163. * To keep stack sizes in check force programmers to notice if they
  164. * start making this union too large! See struct lsm_network_audit
  165. * as an example of how to deal with large data.
  166. */
  167. BUILD_BUG_ON(sizeof(a->u) > sizeof(void *)*2);
  168. switch (a->type) {
  169. case LSM_AUDIT_DATA_NONE:
  170. return;
  171. case LSM_AUDIT_DATA_IPC:
  172. audit_log_format(ab, " ipc_key=%d ", a->u.ipc_id);
  173. break;
  174. case LSM_AUDIT_DATA_CAP:
  175. audit_log_format(ab, " capability=%d ", a->u.cap);
  176. break;
  177. case LSM_AUDIT_DATA_PATH: {
  178. struct inode *inode;
  179. audit_log_d_path(ab, " path=", &a->u.path);
  180. inode = d_backing_inode(a->u.path.dentry);
  181. if (inode) {
  182. audit_log_format(ab, " dev=");
  183. audit_log_untrustedstring(ab, inode->i_sb->s_id);
  184. audit_log_format(ab, " ino=%lu", inode->i_ino);
  185. }
  186. break;
  187. }
  188. case LSM_AUDIT_DATA_FILE: {
  189. struct inode *inode;
  190. audit_log_d_path(ab, " path=", &a->u.file->f_path);
  191. inode = file_inode(a->u.file);
  192. if (inode) {
  193. audit_log_format(ab, " dev=");
  194. audit_log_untrustedstring(ab, inode->i_sb->s_id);
  195. audit_log_format(ab, " ino=%lu", inode->i_ino);
  196. }
  197. break;
  198. }
  199. case LSM_AUDIT_DATA_IOCTL_OP: {
  200. struct inode *inode;
  201. audit_log_d_path(ab, " path=", &a->u.op->path);
  202. inode = a->u.op->path.dentry->d_inode;
  203. if (inode) {
  204. audit_log_format(ab, " dev=");
  205. audit_log_untrustedstring(ab, inode->i_sb->s_id);
  206. audit_log_format(ab, " ino=%lu", inode->i_ino);
  207. }
  208. audit_log_format(ab, " ioctlcmd=0x%hx", a->u.op->cmd);
  209. break;
  210. }
  211. case LSM_AUDIT_DATA_DENTRY: {
  212. struct inode *inode;
  213. audit_log_format(ab, " name=");
  214. spin_lock(&a->u.dentry->d_lock);
  215. audit_log_untrustedstring(ab, a->u.dentry->d_name.name);
  216. spin_unlock(&a->u.dentry->d_lock);
  217. inode = d_backing_inode(a->u.dentry);
  218. if (inode) {
  219. audit_log_format(ab, " dev=");
  220. audit_log_untrustedstring(ab, inode->i_sb->s_id);
  221. audit_log_format(ab, " ino=%lu", inode->i_ino);
  222. }
  223. break;
  224. }
  225. case LSM_AUDIT_DATA_INODE: {
  226. struct dentry *dentry;
  227. struct inode *inode;
  228. rcu_read_lock();
  229. inode = a->u.inode;
  230. dentry = d_find_alias_rcu(inode);
  231. if (dentry) {
  232. audit_log_format(ab, " name=");
  233. spin_lock(&dentry->d_lock);
  234. audit_log_untrustedstring(ab, dentry->d_name.name);
  235. spin_unlock(&dentry->d_lock);
  236. }
  237. audit_log_format(ab, " dev=");
  238. audit_log_untrustedstring(ab, inode->i_sb->s_id);
  239. audit_log_format(ab, " ino=%lu", inode->i_ino);
  240. rcu_read_unlock();
  241. break;
  242. }
  243. case LSM_AUDIT_DATA_TASK: {
  244. struct task_struct *tsk = a->u.tsk;
  245. if (tsk) {
  246. pid_t pid = task_tgid_nr(tsk);
  247. if (pid) {
  248. char tskcomm[sizeof(tsk->comm)];
  249. audit_log_format(ab, " opid=%d ocomm=", pid);
  250. audit_log_untrustedstring(ab,
  251. get_task_comm(tskcomm, tsk));
  252. }
  253. }
  254. break;
  255. }
  256. case LSM_AUDIT_DATA_NET:
  257. if (a->u.net->sk) {
  258. const struct sock *sk = a->u.net->sk;
  259. const struct unix_sock *u;
  260. struct unix_address *addr;
  261. int len = 0;
  262. char *p = NULL;
  263. switch (sk->sk_family) {
  264. case AF_INET: {
  265. const struct inet_sock *inet = inet_sk(sk);
  266. print_ipv4_addr(ab, inet->inet_rcv_saddr,
  267. inet->inet_sport,
  268. "laddr", "lport");
  269. print_ipv4_addr(ab, inet->inet_daddr,
  270. inet->inet_dport,
  271. "faddr", "fport");
  272. break;
  273. }
  274. #if IS_ENABLED(CONFIG_IPV6)
  275. case AF_INET6: {
  276. const struct inet_sock *inet = inet_sk(sk);
  277. print_ipv6_addr(ab, &sk->sk_v6_rcv_saddr,
  278. inet->inet_sport,
  279. "laddr", "lport");
  280. print_ipv6_addr(ab, &sk->sk_v6_daddr,
  281. inet->inet_dport,
  282. "faddr", "fport");
  283. break;
  284. }
  285. #endif
  286. case AF_UNIX:
  287. u = unix_sk(sk);
  288. addr = smp_load_acquire(&u->addr);
  289. if (!addr)
  290. break;
  291. if (u->path.dentry) {
  292. audit_log_d_path(ab, " path=", &u->path);
  293. break;
  294. }
  295. len = addr->len-sizeof(short);
  296. p = &addr->name->sun_path[0];
  297. audit_log_format(ab, " path=");
  298. if (*p)
  299. audit_log_untrustedstring(ab, p);
  300. else
  301. audit_log_n_hex(ab, p, len);
  302. break;
  303. }
  304. }
  305. switch (a->u.net->family) {
  306. case AF_INET:
  307. print_ipv4_addr(ab, a->u.net->v4info.saddr,
  308. a->u.net->sport,
  309. "saddr", "src");
  310. print_ipv4_addr(ab, a->u.net->v4info.daddr,
  311. a->u.net->dport,
  312. "daddr", "dest");
  313. break;
  314. case AF_INET6:
  315. print_ipv6_addr(ab, &a->u.net->v6info.saddr,
  316. a->u.net->sport,
  317. "saddr", "src");
  318. print_ipv6_addr(ab, &a->u.net->v6info.daddr,
  319. a->u.net->dport,
  320. "daddr", "dest");
  321. break;
  322. }
  323. if (a->u.net->netif > 0) {
  324. struct net_device *dev;
  325. /* NOTE: we always use init's namespace */
  326. dev = dev_get_by_index(&init_net, a->u.net->netif);
  327. if (dev) {
  328. audit_log_format(ab, " netif=%s", dev->name);
  329. dev_put(dev);
  330. }
  331. }
  332. break;
  333. #ifdef CONFIG_KEYS
  334. case LSM_AUDIT_DATA_KEY:
  335. audit_log_format(ab, " key_serial=%u", a->u.key_struct.key);
  336. if (a->u.key_struct.key_desc) {
  337. audit_log_format(ab, " key_desc=");
  338. audit_log_untrustedstring(ab, a->u.key_struct.key_desc);
  339. }
  340. break;
  341. #endif
  342. case LSM_AUDIT_DATA_KMOD:
  343. audit_log_format(ab, " kmod=");
  344. audit_log_untrustedstring(ab, a->u.kmod_name);
  345. break;
  346. case LSM_AUDIT_DATA_IBPKEY: {
  347. struct in6_addr sbn_pfx;
  348. memset(&sbn_pfx.s6_addr, 0,
  349. sizeof(sbn_pfx.s6_addr));
  350. memcpy(&sbn_pfx.s6_addr, &a->u.ibpkey->subnet_prefix,
  351. sizeof(a->u.ibpkey->subnet_prefix));
  352. audit_log_format(ab, " pkey=0x%x subnet_prefix=%pI6c",
  353. a->u.ibpkey->pkey, &sbn_pfx);
  354. break;
  355. }
  356. case LSM_AUDIT_DATA_IBENDPORT:
  357. audit_log_format(ab, " device=%s port_num=%u",
  358. a->u.ibendport->dev_name,
  359. a->u.ibendport->port);
  360. break;
  361. case LSM_AUDIT_DATA_LOCKDOWN:
  362. audit_log_format(ab, " lockdown_reason=\"%s\"",
  363. lockdown_reasons[a->u.reason]);
  364. break;
  365. case LSM_AUDIT_DATA_ANONINODE:
  366. audit_log_format(ab, " anonclass=%s", a->u.anonclass);
  367. break;
  368. case LSM_AUDIT_DATA_NLMSGTYPE:
  369. audit_log_format(ab, " nl-msgtype=%hu", a->u.nlmsg_type);
  370. break;
  371. } /* switch (a->type) */
  372. }
  373. /**
  374. * dump_common_audit_data - helper to dump common audit data
  375. * @ab : the audit buffer
  376. * @a : common audit data
  377. */
  378. static void dump_common_audit_data(struct audit_buffer *ab,
  379. const struct common_audit_data *a)
  380. {
  381. char comm[sizeof(current->comm)];
  382. audit_log_format(ab, " pid=%d comm=", task_tgid_nr(current));
  383. audit_log_untrustedstring(ab, get_task_comm(comm, current));
  384. audit_log_lsm_data(ab, a);
  385. }
  386. /**
  387. * common_lsm_audit - generic LSM auditing function
  388. * @a: auxiliary audit data
  389. * @pre_audit: lsm-specific pre-audit callback
  390. * @post_audit: lsm-specific post-audit callback
  391. *
  392. * setup the audit buffer for common security information
  393. * uses callback to print LSM specific information
  394. */
  395. void common_lsm_audit(struct common_audit_data *a,
  396. void (*pre_audit)(struct audit_buffer *, void *),
  397. void (*post_audit)(struct audit_buffer *, void *))
  398. {
  399. struct audit_buffer *ab;
  400. if (a == NULL)
  401. return;
  402. /* we use GFP_ATOMIC so we won't sleep */
  403. ab = audit_log_start(audit_context(), GFP_ATOMIC | __GFP_NOWARN,
  404. AUDIT_AVC);
  405. if (ab == NULL)
  406. return;
  407. if (pre_audit)
  408. pre_audit(ab, a);
  409. dump_common_audit_data(ab, a);
  410. if (post_audit)
  411. post_audit(ab, a);
  412. audit_log_end(ab);
  413. }