]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - net/xfrm/xfrm_user.c
audit: Add auditing to ipsec
[linux-2.6-omap-h63xx.git] / net / xfrm / xfrm_user.c
index c59a78d2923a5baf23061e8f693d3114b8120109..e5372b11fc8f105bd4ecf6071c0418f71855760c 100644 (file)
@@ -31,6 +31,7 @@
 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
 #include <linux/in6.h>
 #endif
+#include <linux/audit.h>
 
 static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
 {
@@ -211,6 +212,7 @@ static int verify_newsa_info(struct xfrm_usersa_info *p,
        case XFRM_MODE_TRANSPORT:
        case XFRM_MODE_TUNNEL:
        case XFRM_MODE_ROUTEOPTIMIZATION:
+       case XFRM_MODE_BEET:
                break;
 
        default:
@@ -243,11 +245,10 @@ static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
        *props = algo->desc.sadb_alg_id;
 
        len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8;
-       p = kmalloc(len, GFP_KERNEL);
+       p = kmemdup(ualg, len, GFP_KERNEL);
        if (!p)
                return -ENOMEM;
 
-       memcpy(p, ualg, len);
        strcpy(p->alg_name, algo->name);
        *algpp = p;
        return 0;
@@ -262,11 +263,10 @@ static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_a
                return 0;
 
        uencap = RTA_DATA(rta);
-       p = kmalloc(sizeof(*p), GFP_KERNEL);
+       p = kmemdup(uencap, sizeof(*p), GFP_KERNEL);
        if (!p)
                return -ENOMEM;
 
-       memcpy(p, uencap, sizeof(*p));
        *encapp = p;
        return 0;
 }
@@ -304,11 +304,10 @@ static int attach_one_addr(xfrm_address_t **addrpp, struct rtattr *u_arg)
                return 0;
 
        uaddrp = RTA_DATA(rta);
-       p = kmalloc(sizeof(*p), GFP_KERNEL);
+       p = kmemdup(uaddrp, sizeof(*p), GFP_KERNEL);
        if (!p)
                return -ENOMEM;
 
-       memcpy(p, uaddrp, sizeof(*p));
        *addrpp = p;
        return 0;
 }
@@ -322,7 +321,7 @@ static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *
        x->props.replay_window = p->replay_window;
        x->props.reqid = p->reqid;
        x->props.family = p->family;
-       x->props.saddr = p->saddr;
+       memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
        x->props.flags = p->flags;
 }
 
@@ -456,6 +455,9 @@ static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
        else
                err = xfrm_state_update(x);
 
+       xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
+                      AUDIT_MAC_IPSEC_ADDSA, err ? 0 : 1, NULL, x);
+
        if (err < 0) {
                x->km.state = XFRM_STATE_DEAD;
                __xfrm_state_put(x);
@@ -494,6 +496,7 @@ static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
                        goto out;
                }
 
+               err = -ESRCH;
                x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
                                             p->family);
        }
@@ -524,6 +527,10 @@ static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
        }
 
        err = xfrm_state_delete(x);
+
+       xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
+                      AUDIT_MAC_IPSEC_DELSA, err ? 0 : 1, NULL, x);
+
        if (err < 0)
                goto out;
 
@@ -544,7 +551,7 @@ static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
        memcpy(&p->lft, &x->lft, sizeof(p->lft));
        memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
        memcpy(&p->stats, &x->stats, sizeof(p->stats));
-       p->saddr = x->props.saddr;
+       memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
        p->mode = x->props.mode;
        p->replay_window = x->props.replay_window;
        p->reqid = x->props.reqid;
@@ -651,7 +658,6 @@ static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
        if (!skb)
                return ERR_PTR(-ENOMEM);
 
-       NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
        info.in_skb = in_skb;
        info.out_skb = skb;
        info.nlmsg_seq = seq;
@@ -771,7 +777,7 @@ out_noput:
        return err;
 }
 
-static int verify_policy_dir(__u8 dir)
+static int verify_policy_dir(u8 dir)
 {
        switch (dir) {
        case XFRM_POLICY_IN:
@@ -786,7 +792,7 @@ static int verify_policy_dir(__u8 dir)
        return 0;
 }
 
-static int verify_policy_type(__u8 type)
+static int verify_policy_type(u8 type)
 {
        switch (type) {
        case XFRM_POLICY_TYPE_MAIN:
@@ -873,22 +879,57 @@ static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
                t->aalgos = ut->aalgos;
                t->ealgos = ut->ealgos;
                t->calgos = ut->calgos;
+               t->encap_family = ut->family;
        }
 }
 
+static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
+{
+       int i;
+
+       if (nr > XFRM_MAX_DEPTH)
+               return -EINVAL;
+
+       for (i = 0; i < nr; i++) {
+               /* We never validated the ut->family value, so many
+                * applications simply leave it at zero.  The check was
+                * never made and ut->family was ignored because all
+                * templates could be assumed to have the same family as
+                * the policy itself.  Now that we will have ipv4-in-ipv6
+                * and ipv6-in-ipv4 tunnels, this is no longer true.
+                */
+               if (!ut[i].family)
+                       ut[i].family = family;
+
+               switch (ut[i].family) {
+               case AF_INET:
+                       break;
+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
+               case AF_INET6:
+                       break;
+#endif
+               default:
+                       return -EINVAL;
+               };
+       }
+
+       return 0;
+}
+
 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
 {
        struct rtattr *rt = xfrma[XFRMA_TMPL-1];
-       struct xfrm_user_tmpl *utmpl;
-       int nr;
 
        if (!rt) {
                pol->xfrm_nr = 0;
        } else {
-               nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
+               struct xfrm_user_tmpl *utmpl = RTA_DATA(rt);
+               int nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
+               int err;
 
-               if (nr > XFRM_MAX_DEPTH)
-                       return -EINVAL;
+               err = validate_tmpl(nr, utmpl, pol->family);
+               if (err)
+                       return err;
 
                copy_templates(pol, RTA_DATA(rt), nr);
        }
@@ -899,7 +940,7 @@ static int copy_from_user_policy_type(u8 *tp, struct rtattr **xfrma)
 {
        struct rtattr *rt = xfrma[XFRMA_POLICY_TYPE-1];
        struct xfrm_userpolicy_type *upt;
-       __u8 type = XFRM_POLICY_TYPE_MAIN;
+       u8 type = XFRM_POLICY_TYPE_MAIN;
        int err;
 
        if (rt) {
@@ -997,6 +1038,9 @@ static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr
         * a type XFRM_MSG_UPDPOLICY - JHS */
        excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
        err = xfrm_policy_insert(p->dir, xp, excl);
+       xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
+                      AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
+
        if (err) {
                security_xfrm_policy_free(xp);
                kfree(xp);
@@ -1026,7 +1070,7 @@ static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
                struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
 
                memcpy(&up->id, &kp->id, sizeof(up->id));
-               up->family = xp->family;
+               up->family = kp->encap_family;
                memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
                up->reqid = kp->reqid;
                up->mode = kp->mode;
@@ -1081,12 +1125,12 @@ static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *s
 }
 
 #ifdef CONFIG_XFRM_SUB_POLICY
-static int copy_to_user_policy_type(struct xfrm_policy *xp, struct sk_buff *skb)
+static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
 {
        struct xfrm_userpolicy_type upt;
 
        memset(&upt, 0, sizeof(upt));
-       upt.type = xp->type;
+       upt.type = type;
 
        RTA_PUT(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
 
@@ -1097,7 +1141,7 @@ rtattr_failure:
 }
 
 #else
-static inline int copy_to_user_policy_type(struct xfrm_policy *xp, struct sk_buff *skb)
+static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
 {
        return 0;
 }
@@ -1126,7 +1170,7 @@ static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr
                goto nlmsg_failure;
        if (copy_to_user_sec_ctx(xp, skb))
                goto nlmsg_failure;
-       if (copy_to_user_policy_type(xp, skb) < 0)
+       if (copy_to_user_policy_type(xp->type, skb) < 0)
                goto nlmsg_failure;
 
        nlh->nlmsg_len = skb->tail - b;
@@ -1169,7 +1213,6 @@ static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
        if (!skb)
                return ERR_PTR(-ENOMEM);
 
-       NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
        info.in_skb = in_skb;
        info.out_skb = skb;
        info.nlmsg_seq = seq;
@@ -1188,7 +1231,7 @@ static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr
 {
        struct xfrm_policy *xp;
        struct xfrm_userpolicy_id *p;
-       __u8 type = XFRM_POLICY_TYPE_MAIN;
+       u8 type = XFRM_POLICY_TYPE_MAIN;
        int err;
        struct km_event c;
        int delete;
@@ -1225,6 +1268,10 @@ static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr
                xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security, delete);
                security_xfrm_policy_free(&tmp);
        }
+       if (delete)
+               xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
+                              AUDIT_MAC_IPSEC_DELSPD, (xp) ? 1 : 0, xp, NULL);
+
        if (xp == NULL)
                return -ENOENT;
 
@@ -1259,8 +1306,11 @@ static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma
 {
        struct km_event c;
        struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
+       struct xfrm_audit audit_info;
 
-       xfrm_state_flush(p->proto);
+       audit_info.loginuid = NETLINK_CB(skb).loginuid;
+       audit_info.secid = NETLINK_CB(skb).sid;
+       xfrm_state_flush(p->proto, &audit_info);
        c.data.proto = p->proto;
        c.event = nlh->nlmsg_type;
        c.seq = nlh->nlmsg_seq;
@@ -1282,10 +1332,12 @@ static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_eve
        id = NLMSG_DATA(nlh);
        nlh->nlmsg_flags = 0;
 
-       id->sa_id.daddr = x->id.daddr;
+       memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
        id->sa_id.spi = x->id.spi;
        id->sa_id.family = x->props.family;
        id->sa_id.proto = x->id.proto;
+       memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
+       id->reqid = x->props.reqid;
        id->flags = c->data.aevent;
 
        RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
@@ -1406,14 +1458,17 @@ out:
 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
 {
        struct km_event c;
-       __u8 type = XFRM_POLICY_TYPE_MAIN;
+       u8 type = XFRM_POLICY_TYPE_MAIN;
        int err;
+       struct xfrm_audit audit_info;
 
        err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
        if (err)
                return err;
 
-       xfrm_policy_flush(type);
+       audit_info.loginuid = NETLINK_CB(skb).loginuid;
+       audit_info.secid = NETLINK_CB(skb).sid;
+       xfrm_policy_flush(type, &audit_info);
        c.data.type = type;
        c.event = nlh->nlmsg_type;
        c.seq = nlh->nlmsg_seq;
@@ -1427,7 +1482,7 @@ static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void *
        struct xfrm_policy *xp;
        struct xfrm_user_polexpire *up = NLMSG_DATA(nlh);
        struct xfrm_userpolicy_info *p = &up->pol;
-       __u8 type = XFRM_POLICY_TYPE_MAIN;
+       u8 type = XFRM_POLICY_TYPE_MAIN;
        int err = -ENOENT;
 
        err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
@@ -1468,6 +1523,9 @@ static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void *
        err = 0;
        if (up->hard) {
                xfrm_policy_delete(xp, p->dir);
+               xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
+                               AUDIT_MAC_IPSEC_DELSPD, 1, xp, NULL);
+
        } else {
                // reset the timers here?
                printk("Dont know what to do with soft policy expire\n");
@@ -1499,8 +1557,11 @@ static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **
                goto out;
        km_state_expired(x, ue->hard, current->pid);
 
-       if (ue->hard)
+       if (ue->hard) {
                __xfrm_state_delete(x);
+               xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
+                              AUDIT_MAC_IPSEC_DELSA, 1, NULL, x);
+       }
 out:
        spin_unlock_bh(&x->lock);
        xfrm_state_put(x);
@@ -1529,7 +1590,8 @@ static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xf
        }
 
        /*   build an XP */
-       xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err);        if (!xp) {
+       xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err);
+       if (!xp) {
                kfree(x);
                return err;
        }
@@ -1906,7 +1968,7 @@ static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
                goto nlmsg_failure;
        if (copy_to_user_state_sec_ctx(x, skb))
                goto nlmsg_failure;
-       if (copy_to_user_policy_type(xp, skb) < 0)
+       if (copy_to_user_policy_type(xp->type, skb) < 0)
                goto nlmsg_failure;
 
        nlh->nlmsg_len = skb->tail - b;
@@ -1926,6 +1988,9 @@ static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
        len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
        len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
        len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
+#ifdef CONFIG_XFRM_SUB_POLICY
+       len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
+#endif
        skb = alloc_skb(len, GFP_ATOMIC);
        if (skb == NULL)
                return -ENOMEM;
@@ -1975,7 +2040,7 @@ static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
                return NULL;
 
        nr = ((len - sizeof(*p)) / sizeof(*ut));
-       if (nr > XFRM_MAX_DEPTH)
+       if (validate_tmpl(nr, ut, p->sel.family))
                return NULL;
 
        if (p->dir > XFRM_POLICY_OUT)
@@ -1991,15 +2056,6 @@ static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
        xp->type = XFRM_POLICY_TYPE_MAIN;
        copy_templates(xp, ut, nr);
 
-       if (!xp->security) {
-               int err = security_xfrm_sock_policy_alloc(xp, sk);
-               if (err) {
-                       kfree(xp);
-                       *dir = err;
-                       return NULL;
-               }
-       }
-
        *dir = p->dir;
 
        return xp;
@@ -2022,7 +2078,7 @@ static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
                goto nlmsg_failure;
        if (copy_to_user_sec_ctx(xp, skb))
                goto nlmsg_failure;
-       if (copy_to_user_policy_type(xp, skb) < 0)
+       if (copy_to_user_policy_type(xp->type, skb) < 0)
                goto nlmsg_failure;
        upe->hard = !!hard;
 
@@ -2042,6 +2098,9 @@ static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_eve
        len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
        len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
        len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
+#ifdef CONFIG_XFRM_SUB_POLICY
+       len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
+#endif
        skb = alloc_skb(len, GFP_ATOMIC);
        if (skb == NULL)
                return -ENOMEM;
@@ -2068,6 +2127,9 @@ static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *
                len += RTA_SPACE(headlen);
                headlen = sizeof(*id);
        }
+#ifdef CONFIG_XFRM_SUB_POLICY
+       len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
+#endif
        len += NLMSG_SPACE(headlen);
 
        skb = alloc_skb(len, GFP_ATOMIC);
@@ -2095,7 +2157,7 @@ static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *
        copy_to_user_policy(xp, p, dir);
        if (copy_to_user_tmpl(xp, skb) < 0)
                goto nlmsg_failure;
-       if (copy_to_user_policy_type(xp, skb) < 0)
+       if (copy_to_user_policy_type(xp->type, skb) < 0)
                goto nlmsg_failure;
 
        nlh->nlmsg_len = skb->tail - b;
@@ -2114,10 +2176,11 @@ static int xfrm_notify_policy_flush(struct km_event *c)
        struct nlmsghdr *nlh;
        struct sk_buff *skb;
        unsigned char *b;
+       int len = 0;
 #ifdef CONFIG_XFRM_SUB_POLICY
-       struct xfrm_userpolicy_type upt;
+       len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
 #endif
-       int len = NLMSG_LENGTH(0);
+       len += NLMSG_LENGTH(0);
 
        skb = alloc_skb(len, GFP_ATOMIC);
        if (skb == NULL)
@@ -2127,12 +2190,8 @@ static int xfrm_notify_policy_flush(struct km_event *c)
 
        nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
        nlh->nlmsg_flags = 0;
-
-#ifdef CONFIG_XFRM_SUB_POLICY
-       memset(&upt, 0, sizeof(upt));
-       upt.type = c->data.type;
-       RTA_PUT(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
-#endif
+       if (copy_to_user_policy_type(c->data.type, skb) < 0)
+               goto nlmsg_failure;
 
        nlh->nlmsg_len = skb->tail - b;
 
@@ -2140,9 +2199,6 @@ static int xfrm_notify_policy_flush(struct km_event *c)
        return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
 
 nlmsg_failure:
-#ifdef CONFIG_XFRM_SUB_POLICY
-rtattr_failure:
-#endif
        kfree_skb(skb);
        return -1;
 }