aboutsummaryrefslogtreecommitdiffstats
path: root/net/xfrm
diff options
context:
space:
mode:
authorDavid S. Miller <davem@davemloft.net>2012-06-28 00:57:03 -0400
committerDavid S. Miller <davem@davemloft.net>2012-06-28 00:57:03 -0400
commit1d1e34ddd48d27def2f324c1e3be16d460b16436 (patch)
tree79937379cbc0498cb3a611df4d14362dbe45f6cd /net/xfrm
parent160c85f0e050d92c0e42321aab2ffc343594e1c0 (diff)
xfrm_user: Propagate netlink error codes properly.
Instead of using a fixed value of "-1" or "-EMSGSIZE", propagate what the nla_*() interfaces actually return. Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net/xfrm')
-rw-r--r--net/xfrm/xfrm_user.c394
1 files changed, 204 insertions, 190 deletions
diff --git a/net/xfrm/xfrm_user.c b/net/xfrm/xfrm_user.c
index 44293b3fd6a1..540762726aaf 100644
--- a/net/xfrm/xfrm_user.c
+++ b/net/xfrm/xfrm_user.c
@@ -754,58 +754,67 @@ static int copy_to_user_state_extra(struct xfrm_state *x,
754 struct xfrm_usersa_info *p, 754 struct xfrm_usersa_info *p,
755 struct sk_buff *skb) 755 struct sk_buff *skb)
756{ 756{
757 copy_to_user_state(x, p); 757 int ret = 0;
758
759 if (x->coaddr &&
760 nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr))
761 goto nla_put_failure;
762
763 if (x->lastused &&
764 nla_put_u64(skb, XFRMA_LASTUSED, x->lastused))
765 goto nla_put_failure;
766
767 if (x->aead &&
768 nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead))
769 goto nla_put_failure;
770
771 if (x->aalg &&
772 (copy_to_user_auth(x->aalg, skb) ||
773 nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
774 xfrm_alg_auth_len(x->aalg), x->aalg)))
775 goto nla_put_failure;
776
777 if (x->ealg &&
778 nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg))
779 goto nla_put_failure;
780
781 if (x->calg &&
782 nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg))
783 goto nla_put_failure;
784
785 if (x->encap &&
786 nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap))
787 goto nla_put_failure;
788 758
789 if (x->tfcpad && 759 copy_to_user_state(x, p);
790 nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad))
791 goto nla_put_failure;
792
793 if (xfrm_mark_put(skb, &x->mark))
794 goto nla_put_failure;
795
796 if (x->replay_esn &&
797 nla_put(skb, XFRMA_REPLAY_ESN_VAL,
798 xfrm_replay_state_esn_len(x->replay_esn),
799 x->replay_esn))
800 goto nla_put_failure;
801
802 if (x->security && copy_sec_ctx(x->security, skb))
803 goto nla_put_failure;
804
805 return 0;
806 760
807nla_put_failure: 761 if (x->coaddr) {
808 return -EMSGSIZE; 762 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
763 if (ret)
764 goto out;
765 }
766 if (x->lastused) {
767 ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
768 if (ret)
769 goto out;
770 }
771 if (x->aead) {
772 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
773 if (ret)
774 goto out;
775 }
776 if (x->aalg) {
777 ret = copy_to_user_auth(x->aalg, skb);
778 if (!ret)
779 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
780 xfrm_alg_auth_len(x->aalg), x->aalg);
781 if (ret)
782 goto out;
783 }
784 if (x->ealg) {
785 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
786 if (ret)
787 goto out;
788 }
789 if (x->calg) {
790 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
791 if (ret)
792 goto out;
793 }
794 if (x->encap) {
795 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
796 if (ret)
797 goto out;
798 }
799 if (x->tfcpad) {
800 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
801 if (ret)
802 goto out;
803 }
804 ret = xfrm_mark_put(skb, &x->mark);
805 if (ret)
806 goto out;
807 if (x->replay_esn) {
808 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
809 xfrm_replay_state_esn_len(x->replay_esn),
810 x->replay_esn);
811 if (ret)
812 goto out;
813 }
814 if (x->security)
815 ret = copy_sec_ctx(x->security, skb);
816out:
817 return ret;
809} 818}
810 819
811static int dump_one_state(struct xfrm_state *x, int count, void *ptr) 820static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
@@ -825,15 +834,12 @@ static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
825 p = nlmsg_data(nlh); 834 p = nlmsg_data(nlh);
826 835
827 err = copy_to_user_state_extra(x, p, skb); 836 err = copy_to_user_state_extra(x, p, skb);
828 if (err) 837 if (err) {
829 goto nla_put_failure; 838 nlmsg_cancel(skb, nlh);
830 839 return err;
840 }
831 nlmsg_end(skb, nlh); 841 nlmsg_end(skb, nlh);
832 return 0; 842 return 0;
833
834nla_put_failure:
835 nlmsg_cancel(skb, nlh);
836 return err;
837} 843}
838 844
839static int xfrm_dump_sa_done(struct netlink_callback *cb) 845static int xfrm_dump_sa_done(struct netlink_callback *cb)
@@ -904,6 +910,7 @@ static int build_spdinfo(struct sk_buff *skb, struct net *net,
904 struct xfrmu_spdinfo spc; 910 struct xfrmu_spdinfo spc;
905 struct xfrmu_spdhinfo sph; 911 struct xfrmu_spdhinfo sph;
906 struct nlmsghdr *nlh; 912 struct nlmsghdr *nlh;
913 int err;
907 u32 *f; 914 u32 *f;
908 915
909 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0); 916 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
@@ -922,15 +929,15 @@ static int build_spdinfo(struct sk_buff *skb, struct net *net,
922 sph.spdhcnt = si.spdhcnt; 929 sph.spdhcnt = si.spdhcnt;
923 sph.spdhmcnt = si.spdhmcnt; 930 sph.spdhmcnt = si.spdhmcnt;
924 931
925 if (nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc) || 932 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
926 nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph)) 933 if (!err)
927 goto nla_put_failure; 934 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
935 if (err) {
936 nlmsg_cancel(skb, nlh);
937 return err;
938 }
928 939
929 return nlmsg_end(skb, nlh); 940 return nlmsg_end(skb, nlh);
930
931nla_put_failure:
932 nlmsg_cancel(skb, nlh);
933 return -EMSGSIZE;
934} 941}
935 942
936static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh, 943static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
@@ -965,6 +972,7 @@ static int build_sadinfo(struct sk_buff *skb, struct net *net,
965 struct xfrmk_sadinfo si; 972 struct xfrmk_sadinfo si;
966 struct xfrmu_sadhinfo sh; 973 struct xfrmu_sadhinfo sh;
967 struct nlmsghdr *nlh; 974 struct nlmsghdr *nlh;
975 int err;
968 u32 *f; 976 u32 *f;
969 977
970 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0); 978 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
@@ -978,15 +986,15 @@ static int build_sadinfo(struct sk_buff *skb, struct net *net,
978 sh.sadhmcnt = si.sadhmcnt; 986 sh.sadhmcnt = si.sadhmcnt;
979 sh.sadhcnt = si.sadhcnt; 987 sh.sadhcnt = si.sadhcnt;
980 988
981 if (nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt) || 989 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
982 nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh)) 990 if (!err)
983 goto nla_put_failure; 991 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
992 if (err) {
993 nlmsg_cancel(skb, nlh);
994 return err;
995 }
984 996
985 return nlmsg_end(skb, nlh); 997 return nlmsg_end(skb, nlh);
986
987nla_put_failure:
988 nlmsg_cancel(skb, nlh);
989 return -EMSGSIZE;
990} 998}
991 999
992static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh, 1000static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
@@ -1439,9 +1447,8 @@ static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buf
1439 1447
1440static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb) 1448static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1441{ 1449{
1442 if (xp->security) { 1450 if (xp->security)
1443 return copy_sec_ctx(xp->security, skb); 1451 return copy_sec_ctx(xp->security, skb);
1444 }
1445 return 0; 1452 return 0;
1446} 1453}
1447static inline size_t userpolicy_type_attrsize(void) 1454static inline size_t userpolicy_type_attrsize(void)
@@ -1477,6 +1484,7 @@ static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr
1477 struct sk_buff *in_skb = sp->in_skb; 1484 struct sk_buff *in_skb = sp->in_skb;
1478 struct sk_buff *skb = sp->out_skb; 1485 struct sk_buff *skb = sp->out_skb;
1479 struct nlmsghdr *nlh; 1486 struct nlmsghdr *nlh;
1487 int err;
1480 1488
1481 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq, 1489 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1482 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags); 1490 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
@@ -1485,22 +1493,19 @@ static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr
1485 1493
1486 p = nlmsg_data(nlh); 1494 p = nlmsg_data(nlh);
1487 copy_to_user_policy(xp, p, dir); 1495 copy_to_user_policy(xp, p, dir);
1488 if (copy_to_user_tmpl(xp, skb) < 0) 1496 err = copy_to_user_tmpl(xp, skb);
1489 goto nlmsg_failure; 1497 if (!err)
1490 if (copy_to_user_sec_ctx(xp, skb)) 1498 err = copy_to_user_sec_ctx(xp, skb);
1491 goto nlmsg_failure; 1499 if (!err)
1492 if (copy_to_user_policy_type(xp->type, skb) < 0) 1500 err = copy_to_user_policy_type(xp->type, skb);
1493 goto nlmsg_failure; 1501 if (!err)
1494 if (xfrm_mark_put(skb, &xp->mark)) 1502 err = xfrm_mark_put(skb, &xp->mark);
1495 goto nla_put_failure; 1503 if (err) {
1496 1504 nlmsg_cancel(skb, nlh);
1505 return err;
1506 }
1497 nlmsg_end(skb, nlh); 1507 nlmsg_end(skb, nlh);
1498 return 0; 1508 return 0;
1499
1500nla_put_failure:
1501nlmsg_failure:
1502 nlmsg_cancel(skb, nlh);
1503 return -EMSGSIZE;
1504} 1509}
1505 1510
1506static int xfrm_dump_policy_done(struct netlink_callback *cb) 1511static int xfrm_dump_policy_done(struct netlink_callback *cb)
@@ -1688,6 +1693,7 @@ static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct
1688{ 1693{
1689 struct xfrm_aevent_id *id; 1694 struct xfrm_aevent_id *id;
1690 struct nlmsghdr *nlh; 1695 struct nlmsghdr *nlh;
1696 int err;
1691 1697
1692 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0); 1698 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1693 if (nlh == NULL) 1699 if (nlh == NULL)
@@ -1703,35 +1709,39 @@ static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct
1703 id->flags = c->data.aevent; 1709 id->flags = c->data.aevent;
1704 1710
1705 if (x->replay_esn) { 1711 if (x->replay_esn) {
1706 if (nla_put(skb, XFRMA_REPLAY_ESN_VAL, 1712 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1707 xfrm_replay_state_esn_len(x->replay_esn), 1713 xfrm_replay_state_esn_len(x->replay_esn),
1708 x->replay_esn)) 1714 x->replay_esn);
1709 goto nla_put_failure;
1710 } else { 1715 } else {
1711 if (nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), 1716 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1712 &x->replay)) 1717 &x->replay);
1713 goto nla_put_failure;
1714 } 1718 }
1715 if (nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft)) 1719 if (err)
1716 goto nla_put_failure; 1720 goto out_cancel;
1717 1721 err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1718 if ((id->flags & XFRM_AE_RTHR) && 1722 if (err)
1719 nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff)) 1723 goto out_cancel;
1720 goto nla_put_failure;
1721
1722 if ((id->flags & XFRM_AE_ETHR) &&
1723 nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1724 x->replay_maxage * 10 / HZ))
1725 goto nla_put_failure;
1726 1724
1727 if (xfrm_mark_put(skb, &x->mark)) 1725 if (id->flags & XFRM_AE_RTHR) {
1728 goto nla_put_failure; 1726 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1727 if (err)
1728 goto out_cancel;
1729 }
1730 if (id->flags & XFRM_AE_ETHR) {
1731 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1732 x->replay_maxage * 10 / HZ);
1733 if (err)
1734 goto out_cancel;
1735 }
1736 err = xfrm_mark_put(skb, &x->mark);
1737 if (err)
1738 goto out_cancel;
1729 1739
1730 return nlmsg_end(skb, nlh); 1740 return nlmsg_end(skb, nlh);
1731 1741
1732nla_put_failure: 1742out_cancel:
1733 nlmsg_cancel(skb, nlh); 1743 nlmsg_cancel(skb, nlh);
1734 return -EMSGSIZE; 1744 return err;
1735} 1745}
1736 1746
1737static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, 1747static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
@@ -2155,7 +2165,7 @@ static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2155 const struct xfrm_migrate *mp; 2165 const struct xfrm_migrate *mp;
2156 struct xfrm_userpolicy_id *pol_id; 2166 struct xfrm_userpolicy_id *pol_id;
2157 struct nlmsghdr *nlh; 2167 struct nlmsghdr *nlh;
2158 int i; 2168 int i, err;
2159 2169
2160 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0); 2170 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2161 if (nlh == NULL) 2171 if (nlh == NULL)
@@ -2167,21 +2177,25 @@ static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2167 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel)); 2177 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2168 pol_id->dir = dir; 2178 pol_id->dir = dir;
2169 2179
2170 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0)) 2180 if (k != NULL) {
2171 goto nlmsg_failure; 2181 err = copy_to_user_kmaddress(k, skb);
2172 2182 if (err)
2173 if (copy_to_user_policy_type(type, skb) < 0) 2183 goto out_cancel;
2174 goto nlmsg_failure; 2184 }
2175 2185 err = copy_to_user_policy_type(type, skb);
2186 if (err)
2187 goto out_cancel;
2176 for (i = 0, mp = m ; i < num_migrate; i++, mp++) { 2188 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2177 if (copy_to_user_migrate(mp, skb) < 0) 2189 err = copy_to_user_migrate(mp, skb);
2178 goto nlmsg_failure; 2190 if (err)
2191 goto out_cancel;
2179 } 2192 }
2180 2193
2181 return nlmsg_end(skb, nlh); 2194 return nlmsg_end(skb, nlh);
2182nlmsg_failure: 2195
2196out_cancel:
2183 nlmsg_cancel(skb, nlh); 2197 nlmsg_cancel(skb, nlh);
2184 return -EMSGSIZE; 2198 return err;
2185} 2199}
2186 2200
2187static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, 2201static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
@@ -2354,6 +2368,7 @@ static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct
2354{ 2368{
2355 struct xfrm_user_expire *ue; 2369 struct xfrm_user_expire *ue;
2356 struct nlmsghdr *nlh; 2370 struct nlmsghdr *nlh;
2371 int err;
2357 2372
2358 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0); 2373 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2359 if (nlh == NULL) 2374 if (nlh == NULL)
@@ -2363,13 +2378,11 @@ static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct
2363 copy_to_user_state(x, &ue->state); 2378 copy_to_user_state(x, &ue->state);
2364 ue->hard = (c->data.hard != 0) ? 1 : 0; 2379 ue->hard = (c->data.hard != 0) ? 1 : 0;
2365 2380
2366 if (xfrm_mark_put(skb, &x->mark)) 2381 err = xfrm_mark_put(skb, &x->mark);
2367 goto nla_put_failure; 2382 if (err)
2383 return err;
2368 2384
2369 return nlmsg_end(skb, nlh); 2385 return nlmsg_end(skb, nlh);
2370
2371nla_put_failure:
2372 return -EMSGSIZE;
2373} 2386}
2374 2387
2375static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c) 2388static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
@@ -2470,7 +2483,7 @@ static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2470 struct nlmsghdr *nlh; 2483 struct nlmsghdr *nlh;
2471 struct sk_buff *skb; 2484 struct sk_buff *skb;
2472 int len = xfrm_sa_len(x); 2485 int len = xfrm_sa_len(x);
2473 int headlen; 2486 int headlen, err;
2474 2487
2475 headlen = sizeof(*p); 2488 headlen = sizeof(*p);
2476 if (c->event == XFRM_MSG_DELSA) { 2489 if (c->event == XFRM_MSG_DELSA) {
@@ -2485,8 +2498,9 @@ static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2485 return -ENOMEM; 2498 return -ENOMEM;
2486 2499
2487 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0); 2500 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2501 err = -EMSGSIZE;
2488 if (nlh == NULL) 2502 if (nlh == NULL)
2489 goto nla_put_failure; 2503 goto out_free_skb;
2490 2504
2491 p = nlmsg_data(nlh); 2505 p = nlmsg_data(nlh);
2492 if (c->event == XFRM_MSG_DELSA) { 2506 if (c->event == XFRM_MSG_DELSA) {
@@ -2499,24 +2513,23 @@ static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2499 id->proto = x->id.proto; 2513 id->proto = x->id.proto;
2500 2514
2501 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p)); 2515 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2516 err = -EMSGSIZE;
2502 if (attr == NULL) 2517 if (attr == NULL)
2503 goto nla_put_failure; 2518 goto out_free_skb;
2504 2519
2505 p = nla_data(attr); 2520 p = nla_data(attr);
2506 } 2521 }
2507 2522 err = copy_to_user_state_extra(x, p, skb);
2508 if (copy_to_user_state_extra(x, p, skb)) 2523 if (err)
2509 goto nla_put_failure; 2524 goto out_free_skb;
2510 2525
2511 nlmsg_end(skb, nlh); 2526 nlmsg_end(skb, nlh);
2512 2527
2513 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC); 2528 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2514 2529
2515nla_put_failure: 2530out_free_skb:
2516 /* Somebody screwed up with xfrm_sa_len! */
2517 WARN_ON(1);
2518 kfree_skb(skb); 2531 kfree_skb(skb);
2519 return -1; 2532 return err;
2520} 2533}
2521 2534
2522static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c) 2535static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
@@ -2557,9 +2570,10 @@ static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2557 struct xfrm_tmpl *xt, struct xfrm_policy *xp, 2570 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2558 int dir) 2571 int dir)
2559{ 2572{
2573 __u32 seq = xfrm_get_acqseq();
2560 struct xfrm_user_acquire *ua; 2574 struct xfrm_user_acquire *ua;
2561 struct nlmsghdr *nlh; 2575 struct nlmsghdr *nlh;
2562 __u32 seq = xfrm_get_acqseq(); 2576 int err;
2563 2577
2564 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0); 2578 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2565 if (nlh == NULL) 2579 if (nlh == NULL)
@@ -2575,21 +2589,19 @@ static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2575 ua->calgos = xt->calgos; 2589 ua->calgos = xt->calgos;
2576 ua->seq = x->km.seq = seq; 2590 ua->seq = x->km.seq = seq;
2577 2591
2578 if (copy_to_user_tmpl(xp, skb) < 0) 2592 err = copy_to_user_tmpl(xp, skb);
2579 goto nlmsg_failure; 2593 if (!err)
2580 if (copy_to_user_state_sec_ctx(x, skb)) 2594 err = copy_to_user_state_sec_ctx(x, skb);
2581 goto nlmsg_failure; 2595 if (!err)
2582 if (copy_to_user_policy_type(xp->type, skb) < 0) 2596 err = copy_to_user_policy_type(xp->type, skb);
2583 goto nlmsg_failure; 2597 if (!err)
2584 if (xfrm_mark_put(skb, &xp->mark)) 2598 err = xfrm_mark_put(skb, &xp->mark);
2585 goto nla_put_failure; 2599 if (err) {
2600 nlmsg_cancel(skb, nlh);
2601 return err;
2602 }
2586 2603
2587 return nlmsg_end(skb, nlh); 2604 return nlmsg_end(skb, nlh);
2588
2589nla_put_failure:
2590nlmsg_failure:
2591 nlmsg_cancel(skb, nlh);
2592 return -EMSGSIZE;
2593} 2605}
2594 2606
2595static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt, 2607static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
@@ -2681,8 +2693,9 @@ static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2681 int dir, const struct km_event *c) 2693 int dir, const struct km_event *c)
2682{ 2694{
2683 struct xfrm_user_polexpire *upe; 2695 struct xfrm_user_polexpire *upe;
2684 struct nlmsghdr *nlh;
2685 int hard = c->data.hard; 2696 int hard = c->data.hard;
2697 struct nlmsghdr *nlh;
2698 int err;
2686 2699
2687 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0); 2700 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2688 if (nlh == NULL) 2701 if (nlh == NULL)
@@ -2690,22 +2703,20 @@ static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2690 2703
2691 upe = nlmsg_data(nlh); 2704 upe = nlmsg_data(nlh);
2692 copy_to_user_policy(xp, &upe->pol, dir); 2705 copy_to_user_policy(xp, &upe->pol, dir);
2693 if (copy_to_user_tmpl(xp, skb) < 0) 2706 err = copy_to_user_tmpl(xp, skb);
2694 goto nlmsg_failure; 2707 if (!err)
2695 if (copy_to_user_sec_ctx(xp, skb)) 2708 err = copy_to_user_sec_ctx(xp, skb);
2696 goto nlmsg_failure; 2709 if (!err)
2697 if (copy_to_user_policy_type(xp->type, skb) < 0) 2710 err = copy_to_user_policy_type(xp->type, skb);
2698 goto nlmsg_failure; 2711 if (!err)
2699 if (xfrm_mark_put(skb, &xp->mark)) 2712 err = xfrm_mark_put(skb, &xp->mark);
2700 goto nla_put_failure; 2713 if (err) {
2714 nlmsg_cancel(skb, nlh);
2715 return err;
2716 }
2701 upe->hard = !!hard; 2717 upe->hard = !!hard;
2702 2718
2703 return nlmsg_end(skb, nlh); 2719 return nlmsg_end(skb, nlh);
2704
2705nla_put_failure:
2706nlmsg_failure:
2707 nlmsg_cancel(skb, nlh);
2708 return -EMSGSIZE;
2709} 2720}
2710 2721
2711static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c) 2722static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
@@ -2725,13 +2736,13 @@ static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct
2725 2736
2726static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c) 2737static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2727{ 2738{
2739 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2728 struct net *net = xp_net(xp); 2740 struct net *net = xp_net(xp);
2729 struct xfrm_userpolicy_info *p; 2741 struct xfrm_userpolicy_info *p;
2730 struct xfrm_userpolicy_id *id; 2742 struct xfrm_userpolicy_id *id;
2731 struct nlmsghdr *nlh; 2743 struct nlmsghdr *nlh;
2732 struct sk_buff *skb; 2744 struct sk_buff *skb;
2733 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr); 2745 int headlen, err;
2734 int headlen;
2735 2746
2736 headlen = sizeof(*p); 2747 headlen = sizeof(*p);
2737 if (c->event == XFRM_MSG_DELPOLICY) { 2748 if (c->event == XFRM_MSG_DELPOLICY) {
@@ -2747,8 +2758,9 @@ static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_e
2747 return -ENOMEM; 2758 return -ENOMEM;
2748 2759
2749 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0); 2760 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2761 err = -EMSGSIZE;
2750 if (nlh == NULL) 2762 if (nlh == NULL)
2751 goto nlmsg_failure; 2763 goto out_free_skb;
2752 2764
2753 p = nlmsg_data(nlh); 2765 p = nlmsg_data(nlh);
2754 if (c->event == XFRM_MSG_DELPOLICY) { 2766 if (c->event == XFRM_MSG_DELPOLICY) {
@@ -2763,29 +2775,29 @@ static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_e
2763 memcpy(&id->sel, &xp->selector, sizeof(id->sel)); 2775 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2764 2776
2765 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p)); 2777 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2778 err = -EMSGSIZE;
2766 if (attr == NULL) 2779 if (attr == NULL)
2767 goto nlmsg_failure; 2780 goto out_free_skb;
2768 2781
2769 p = nla_data(attr); 2782 p = nla_data(attr);
2770 } 2783 }
2771 2784
2772 copy_to_user_policy(xp, p, dir); 2785 copy_to_user_policy(xp, p, dir);
2773 if (copy_to_user_tmpl(xp, skb) < 0) 2786 err = copy_to_user_tmpl(xp, skb);
2774 goto nlmsg_failure; 2787 if (!err)
2775 if (copy_to_user_policy_type(xp->type, skb) < 0) 2788 err = copy_to_user_policy_type(xp->type, skb);
2776 goto nlmsg_failure; 2789 if (!err)
2777 2790 err = xfrm_mark_put(skb, &xp->mark);
2778 if (xfrm_mark_put(skb, &xp->mark)) 2791 if (err)
2779 goto nla_put_failure; 2792 goto out_free_skb;
2780 2793
2781 nlmsg_end(skb, nlh); 2794 nlmsg_end(skb, nlh);
2782 2795
2783 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC); 2796 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2784 2797
2785nla_put_failure: 2798out_free_skb:
2786nlmsg_failure:
2787 kfree_skb(skb); 2799 kfree_skb(skb);
2788 return -1; 2800 return err;
2789} 2801}
2790 2802
2791static int xfrm_notify_policy_flush(const struct km_event *c) 2803static int xfrm_notify_policy_flush(const struct km_event *c)
@@ -2793,24 +2805,27 @@ static int xfrm_notify_policy_flush(const struct km_event *c)
2793 struct net *net = c->net; 2805 struct net *net = c->net;
2794 struct nlmsghdr *nlh; 2806 struct nlmsghdr *nlh;
2795 struct sk_buff *skb; 2807 struct sk_buff *skb;
2808 int err;
2796 2809
2797 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC); 2810 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2798 if (skb == NULL) 2811 if (skb == NULL)
2799 return -ENOMEM; 2812 return -ENOMEM;
2800 2813
2801 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0); 2814 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2815 err = -EMSGSIZE;
2802 if (nlh == NULL) 2816 if (nlh == NULL)
2803 goto nlmsg_failure; 2817 goto out_free_skb;
2804 if (copy_to_user_policy_type(c->data.type, skb) < 0) 2818 err = copy_to_user_policy_type(c->data.type, skb);
2805 goto nlmsg_failure; 2819 if (err)
2820 goto out_free_skb;
2806 2821
2807 nlmsg_end(skb, nlh); 2822 nlmsg_end(skb, nlh);
2808 2823
2809 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC); 2824 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2810 2825
2811nlmsg_failure: 2826out_free_skb:
2812 kfree_skb(skb); 2827 kfree_skb(skb);
2813 return -1; 2828 return err;
2814} 2829}
2815 2830
2816static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c) 2831static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
@@ -2853,15 +2868,14 @@ static int build_report(struct sk_buff *skb, u8 proto,
2853 ur->proto = proto; 2868 ur->proto = proto;
2854 memcpy(&ur->sel, sel, sizeof(ur->sel)); 2869 memcpy(&ur->sel, sel, sizeof(ur->sel));
2855 2870
2856 if (addr && 2871 if (addr) {
2857 nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr)) 2872 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
2858 goto nla_put_failure; 2873 if (err) {
2859 2874 nlmsg_cancel(skb, nlh);
2875 return err;
2876 }
2877 }
2860 return nlmsg_end(skb, nlh); 2878 return nlmsg_end(skb, nlh);
2861
2862nla_put_failure:
2863 nlmsg_cancel(skb, nlh);
2864 return -EMSGSIZE;
2865} 2879}
2866 2880
2867static int xfrm_send_report(struct net *net, u8 proto, 2881static int xfrm_send_report(struct net *net, u8 proto,