Back to home page

LXR

 
 

    


0001 /*
0002  * common LSM auditing functions
0003  *
0004  * Based on code written for SELinux by :
0005  *          Stephen Smalley, <sds@epoch.ncsc.mil>
0006  *          James Morris <jmorris@redhat.com>
0007  * Author : Etienne Basset, <etienne.basset@ensta.org>
0008  *
0009  * This program is free software; you can redistribute it and/or modify
0010  * it under the terms of the GNU General Public License version 2,
0011  * as published by the Free Software Foundation.
0012  */
0013 
0014 #include <linux/types.h>
0015 #include <linux/stddef.h>
0016 #include <linux/kernel.h>
0017 #include <linux/gfp.h>
0018 #include <linux/fs.h>
0019 #include <linux/init.h>
0020 #include <net/sock.h>
0021 #include <linux/un.h>
0022 #include <net/af_unix.h>
0023 #include <linux/audit.h>
0024 #include <linux/ipv6.h>
0025 #include <linux/ip.h>
0026 #include <net/ip.h>
0027 #include <net/ipv6.h>
0028 #include <linux/tcp.h>
0029 #include <linux/udp.h>
0030 #include <linux/dccp.h>
0031 #include <linux/sctp.h>
0032 #include <linux/lsm_audit.h>
0033 
0034 /**
0035  * ipv4_skb_to_auditdata : fill auditdata from skb
0036  * @skb : the skb
0037  * @ad : the audit data to fill
0038  * @proto : the layer 4 protocol
0039  *
0040  * return  0 on success
0041  */
0042 int ipv4_skb_to_auditdata(struct sk_buff *skb,
0043         struct common_audit_data *ad, u8 *proto)
0044 {
0045     int ret = 0;
0046     struct iphdr *ih;
0047 
0048     ih = ip_hdr(skb);
0049     if (ih == NULL)
0050         return -EINVAL;
0051 
0052     ad->u.net->v4info.saddr = ih->saddr;
0053     ad->u.net->v4info.daddr = ih->daddr;
0054 
0055     if (proto)
0056         *proto = ih->protocol;
0057     /* non initial fragment */
0058     if (ntohs(ih->frag_off) & IP_OFFSET)
0059         return 0;
0060 
0061     switch (ih->protocol) {
0062     case IPPROTO_TCP: {
0063         struct tcphdr *th = tcp_hdr(skb);
0064         if (th == NULL)
0065             break;
0066 
0067         ad->u.net->sport = th->source;
0068         ad->u.net->dport = th->dest;
0069         break;
0070     }
0071     case IPPROTO_UDP: {
0072         struct udphdr *uh = udp_hdr(skb);
0073         if (uh == NULL)
0074             break;
0075 
0076         ad->u.net->sport = uh->source;
0077         ad->u.net->dport = uh->dest;
0078         break;
0079     }
0080     case IPPROTO_DCCP: {
0081         struct dccp_hdr *dh = dccp_hdr(skb);
0082         if (dh == NULL)
0083             break;
0084 
0085         ad->u.net->sport = dh->dccph_sport;
0086         ad->u.net->dport = dh->dccph_dport;
0087         break;
0088     }
0089     case IPPROTO_SCTP: {
0090         struct sctphdr *sh = sctp_hdr(skb);
0091         if (sh == NULL)
0092             break;
0093         ad->u.net->sport = sh->source;
0094         ad->u.net->dport = sh->dest;
0095         break;
0096     }
0097     default:
0098         ret = -EINVAL;
0099     }
0100     return ret;
0101 }
0102 #if IS_ENABLED(CONFIG_IPV6)
0103 /**
0104  * ipv6_skb_to_auditdata : fill auditdata from skb
0105  * @skb : the skb
0106  * @ad : the audit data to fill
0107  * @proto : the layer 4 protocol
0108  *
0109  * return  0 on success
0110  */
0111 int ipv6_skb_to_auditdata(struct sk_buff *skb,
0112         struct common_audit_data *ad, u8 *proto)
0113 {
0114     int offset, ret = 0;
0115     struct ipv6hdr *ip6;
0116     u8 nexthdr;
0117     __be16 frag_off;
0118 
0119     ip6 = ipv6_hdr(skb);
0120     if (ip6 == NULL)
0121         return -EINVAL;
0122     ad->u.net->v6info.saddr = ip6->saddr;
0123     ad->u.net->v6info.daddr = ip6->daddr;
0124     ret = 0;
0125     /* IPv6 can have several extension header before the Transport header
0126      * skip them */
0127     offset = skb_network_offset(skb);
0128     offset += sizeof(*ip6);
0129     nexthdr = ip6->nexthdr;
0130     offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
0131     if (offset < 0)
0132         return 0;
0133     if (proto)
0134         *proto = nexthdr;
0135     switch (nexthdr) {
0136     case IPPROTO_TCP: {
0137         struct tcphdr _tcph, *th;
0138 
0139         th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
0140         if (th == NULL)
0141             break;
0142 
0143         ad->u.net->sport = th->source;
0144         ad->u.net->dport = th->dest;
0145         break;
0146     }
0147     case IPPROTO_UDP: {
0148         struct udphdr _udph, *uh;
0149 
0150         uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
0151         if (uh == NULL)
0152             break;
0153 
0154         ad->u.net->sport = uh->source;
0155         ad->u.net->dport = uh->dest;
0156         break;
0157     }
0158     case IPPROTO_DCCP: {
0159         struct dccp_hdr _dccph, *dh;
0160 
0161         dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
0162         if (dh == NULL)
0163             break;
0164 
0165         ad->u.net->sport = dh->dccph_sport;
0166         ad->u.net->dport = dh->dccph_dport;
0167         break;
0168     }
0169     case IPPROTO_SCTP: {
0170         struct sctphdr _sctph, *sh;
0171 
0172         sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
0173         if (sh == NULL)
0174             break;
0175         ad->u.net->sport = sh->source;
0176         ad->u.net->dport = sh->dest;
0177         break;
0178     }
0179     default:
0180         ret = -EINVAL;
0181     }
0182     return ret;
0183 }
0184 #endif
0185 
0186 
0187 static inline void print_ipv6_addr(struct audit_buffer *ab,
0188                    struct in6_addr *addr, __be16 port,
0189                    char *name1, char *name2)
0190 {
0191     if (!ipv6_addr_any(addr))
0192         audit_log_format(ab, " %s=%pI6c", name1, addr);
0193     if (port)
0194         audit_log_format(ab, " %s=%d", name2, ntohs(port));
0195 }
0196 
0197 static inline void print_ipv4_addr(struct audit_buffer *ab, __be32 addr,
0198                    __be16 port, char *name1, char *name2)
0199 {
0200     if (addr)
0201         audit_log_format(ab, " %s=%pI4", name1, &addr);
0202     if (port)
0203         audit_log_format(ab, " %s=%d", name2, ntohs(port));
0204 }
0205 
0206 /**
0207  * dump_common_audit_data - helper to dump common audit data
0208  * @a : common audit data
0209  *
0210  */
0211 static void dump_common_audit_data(struct audit_buffer *ab,
0212                    struct common_audit_data *a)
0213 {
0214     char comm[sizeof(current->comm)];
0215 
0216     /*
0217      * To keep stack sizes in check force programers to notice if they
0218      * start making this union too large!  See struct lsm_network_audit
0219      * as an example of how to deal with large data.
0220      */
0221     BUILD_BUG_ON(sizeof(a->u) > sizeof(void *)*2);
0222 
0223     audit_log_format(ab, " pid=%d comm=", task_tgid_nr(current));
0224     audit_log_untrustedstring(ab, memcpy(comm, current->comm, sizeof(comm)));
0225 
0226     switch (a->type) {
0227     case LSM_AUDIT_DATA_NONE:
0228         return;
0229     case LSM_AUDIT_DATA_IPC:
0230         audit_log_format(ab, " key=%d ", a->u.ipc_id);
0231         break;
0232     case LSM_AUDIT_DATA_CAP:
0233         audit_log_format(ab, " capability=%d ", a->u.cap);
0234         break;
0235     case LSM_AUDIT_DATA_PATH: {
0236         struct inode *inode;
0237 
0238         audit_log_d_path(ab, " path=", &a->u.path);
0239 
0240         inode = d_backing_inode(a->u.path.dentry);
0241         if (inode) {
0242             audit_log_format(ab, " dev=");
0243             audit_log_untrustedstring(ab, inode->i_sb->s_id);
0244             audit_log_format(ab, " ino=%lu", inode->i_ino);
0245         }
0246         break;
0247     }
0248     case LSM_AUDIT_DATA_FILE: {
0249         struct inode *inode;
0250 
0251         audit_log_d_path(ab, " path=", &a->u.file->f_path);
0252 
0253         inode = file_inode(a->u.file);
0254         if (inode) {
0255             audit_log_format(ab, " dev=");
0256             audit_log_untrustedstring(ab, inode->i_sb->s_id);
0257             audit_log_format(ab, " ino=%lu", inode->i_ino);
0258         }
0259         break;
0260     }
0261     case LSM_AUDIT_DATA_IOCTL_OP: {
0262         struct inode *inode;
0263 
0264         audit_log_d_path(ab, " path=", &a->u.op->path);
0265 
0266         inode = a->u.op->path.dentry->d_inode;
0267         if (inode) {
0268             audit_log_format(ab, " dev=");
0269             audit_log_untrustedstring(ab, inode->i_sb->s_id);
0270             audit_log_format(ab, " ino=%lu", inode->i_ino);
0271         }
0272 
0273         audit_log_format(ab, " ioctlcmd=0x%hx", a->u.op->cmd);
0274         break;
0275     }
0276     case LSM_AUDIT_DATA_DENTRY: {
0277         struct inode *inode;
0278 
0279         audit_log_format(ab, " name=");
0280         audit_log_untrustedstring(ab, a->u.dentry->d_name.name);
0281 
0282         inode = d_backing_inode(a->u.dentry);
0283         if (inode) {
0284             audit_log_format(ab, " dev=");
0285             audit_log_untrustedstring(ab, inode->i_sb->s_id);
0286             audit_log_format(ab, " ino=%lu", inode->i_ino);
0287         }
0288         break;
0289     }
0290     case LSM_AUDIT_DATA_INODE: {
0291         struct dentry *dentry;
0292         struct inode *inode;
0293 
0294         inode = a->u.inode;
0295         dentry = d_find_alias(inode);
0296         if (dentry) {
0297             audit_log_format(ab, " name=");
0298             audit_log_untrustedstring(ab,
0299                      dentry->d_name.name);
0300             dput(dentry);
0301         }
0302         audit_log_format(ab, " dev=");
0303         audit_log_untrustedstring(ab, inode->i_sb->s_id);
0304         audit_log_format(ab, " ino=%lu", inode->i_ino);
0305         break;
0306     }
0307     case LSM_AUDIT_DATA_TASK: {
0308         struct task_struct *tsk = a->u.tsk;
0309         if (tsk) {
0310             pid_t pid = task_tgid_nr(tsk);
0311             if (pid) {
0312                 char comm[sizeof(tsk->comm)];
0313                 audit_log_format(ab, " opid=%d ocomm=", pid);
0314                 audit_log_untrustedstring(ab,
0315                     memcpy(comm, tsk->comm, sizeof(comm)));
0316             }
0317         }
0318         break;
0319     }
0320     case LSM_AUDIT_DATA_NET:
0321         if (a->u.net->sk) {
0322             struct sock *sk = a->u.net->sk;
0323             struct unix_sock *u;
0324             int len = 0;
0325             char *p = NULL;
0326 
0327             switch (sk->sk_family) {
0328             case AF_INET: {
0329                 struct inet_sock *inet = inet_sk(sk);
0330 
0331                 print_ipv4_addr(ab, inet->inet_rcv_saddr,
0332                         inet->inet_sport,
0333                         "laddr", "lport");
0334                 print_ipv4_addr(ab, inet->inet_daddr,
0335                         inet->inet_dport,
0336                         "faddr", "fport");
0337                 break;
0338             }
0339 #if IS_ENABLED(CONFIG_IPV6)
0340             case AF_INET6: {
0341                 struct inet_sock *inet = inet_sk(sk);
0342 
0343                 print_ipv6_addr(ab, &sk->sk_v6_rcv_saddr,
0344                         inet->inet_sport,
0345                         "laddr", "lport");
0346                 print_ipv6_addr(ab, &sk->sk_v6_daddr,
0347                         inet->inet_dport,
0348                         "faddr", "fport");
0349                 break;
0350             }
0351 #endif
0352             case AF_UNIX:
0353                 u = unix_sk(sk);
0354                 if (u->path.dentry) {
0355                     audit_log_d_path(ab, " path=", &u->path);
0356                     break;
0357                 }
0358                 if (!u->addr)
0359                     break;
0360                 len = u->addr->len-sizeof(short);
0361                 p = &u->addr->name->sun_path[0];
0362                 audit_log_format(ab, " path=");
0363                 if (*p)
0364                     audit_log_untrustedstring(ab, p);
0365                 else
0366                     audit_log_n_hex(ab, p, len);
0367                 break;
0368             }
0369         }
0370 
0371         switch (a->u.net->family) {
0372         case AF_INET:
0373             print_ipv4_addr(ab, a->u.net->v4info.saddr,
0374                     a->u.net->sport,
0375                     "saddr", "src");
0376             print_ipv4_addr(ab, a->u.net->v4info.daddr,
0377                     a->u.net->dport,
0378                     "daddr", "dest");
0379             break;
0380         case AF_INET6:
0381             print_ipv6_addr(ab, &a->u.net->v6info.saddr,
0382                     a->u.net->sport,
0383                     "saddr", "src");
0384             print_ipv6_addr(ab, &a->u.net->v6info.daddr,
0385                     a->u.net->dport,
0386                     "daddr", "dest");
0387             break;
0388         }
0389         if (a->u.net->netif > 0) {
0390             struct net_device *dev;
0391 
0392             /* NOTE: we always use init's namespace */
0393             dev = dev_get_by_index(&init_net, a->u.net->netif);
0394             if (dev) {
0395                 audit_log_format(ab, " netif=%s", dev->name);
0396                 dev_put(dev);
0397             }
0398         }
0399         break;
0400 #ifdef CONFIG_KEYS
0401     case LSM_AUDIT_DATA_KEY:
0402         audit_log_format(ab, " key_serial=%u", a->u.key_struct.key);
0403         if (a->u.key_struct.key_desc) {
0404             audit_log_format(ab, " key_desc=");
0405             audit_log_untrustedstring(ab, a->u.key_struct.key_desc);
0406         }
0407         break;
0408 #endif
0409     case LSM_AUDIT_DATA_KMOD:
0410         audit_log_format(ab, " kmod=");
0411         audit_log_untrustedstring(ab, a->u.kmod_name);
0412         break;
0413     } /* switch (a->type) */
0414 }
0415 
0416 /**
0417  * common_lsm_audit - generic LSM auditing function
0418  * @a:  auxiliary audit data
0419  * @pre_audit: lsm-specific pre-audit callback
0420  * @post_audit: lsm-specific post-audit callback
0421  *
0422  * setup the audit buffer for common security information
0423  * uses callback to print LSM specific information
0424  */
0425 void common_lsm_audit(struct common_audit_data *a,
0426     void (*pre_audit)(struct audit_buffer *, void *),
0427     void (*post_audit)(struct audit_buffer *, void *))
0428 {
0429     struct audit_buffer *ab;
0430 
0431     if (a == NULL)
0432         return;
0433     /* we use GFP_ATOMIC so we won't sleep */
0434     ab = audit_log_start(current->audit_context, GFP_ATOMIC | __GFP_NOWARN,
0435                  AUDIT_AVC);
0436 
0437     if (ab == NULL)
0438         return;
0439 
0440     if (pre_audit)
0441         pre_audit(ab, a);
0442 
0443     dump_common_audit_data(ab, a);
0444 
0445     if (post_audit)
0446         post_audit(ab, a);
0447 
0448     audit_log_end(ab);
0449 }