From: Christian Brauner Subject: Re: netlink: 16 bytes leftover after parsing attributes in process `ip'. Date: Wed, 26 Sep 2018 11:49:23 +0200 Message-ID: <20180926094922.y3gg2kzui4j6fvdx@brauner.io> References: <6059bf4e-b1cf-2c7e-5529-9003bdd8a14b@gmail.com> <20180925094908.yuu5bsrazkiq3ihy@brauner.io> <20180925164715.338bb059@redhat.com> <02fbfcb8-537c-dc5c-52dc-0f53d7fd0482@gmail.com> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="rwtverreqta75zyj" Cc: Jiri Benc , "netdev@vger.kernel.org" , David Miller To: David Ahern Return-path: Received: from mail-ed1-f66.google.com ([209.85.208.66]:35210 "EHLO mail-ed1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726401AbeIZQBk (ORCPT ); Wed, 26 Sep 2018 12:01:40 -0400 Received: by mail-ed1-f66.google.com with SMTP id y21-v6so1332923edr.2 for ; Wed, 26 Sep 2018 02:49:30 -0700 (PDT) Content-Disposition: inline In-Reply-To: <02fbfcb8-537c-dc5c-52dc-0f53d7fd0482@gmail.com> Sender: netdev-owner@vger.kernel.org List-ID: --rwtverreqta75zyj Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Sep 25, 2018 at 09:37:41AM -0600, David Ahern wrote: > On 9/25/18 8:47 AM, Jiri Benc wrote: > > On Tue, 25 Sep 2018 11:49:10 +0200, Christian Brauner wrote: > >> So if people really want to hide this issue as much as we can then we > >> can play the guessing game. I could send a patch that roughly does the > >> following: > >> > >> if (nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg)) > >> guessed_header_len =3D sizeof(struct ifaddrmsg); > >> else > >> guessed_header_len =3D sizeof(struct ifinfomsg); > >> > >> This will work since sizeof(ifaddrmsg) =3D=3D 8 and sizeof(ifinfomsg) = =3D=3D 16. > >> The only valid property for RTM_GETADDR requests is IFA_TARGET_NETNSID. > >> This propert is a __s32 which should bring the message up to 12 bytes > >> (not sure about alignment requiremnts and where we might wend up ten) > >> which is still less than the 16 bytes without that property from > >> ifinfomsg. That's a hacky hacky hack-hack and will likely work but will > >> break when ifaddrmsg grows a new member or we introduce another proper= ty > >> that is valid in RTM_GETADDR requests. It also will not work cleanly > >> when users stuff additional properties in there that are vaif (nlmsg_p= arse(cb->nlh, sizeof(struct ifaddrmsg), tb, IFA_MAX,lid for the > >> address family but are not used int RTM_GETADDR requests. > >=20 > > I'd expect that any potential existing code that makes use of other > > attributes already assumes ifaddrmsg. Hence, if the nlmsg_len is > > > sizeof(ifinfomsg), you can be sure that there are attributes and thus > > the struct used was ifaddrmsg. > >=20 > > So, in order for RTM_GETADDR to work reliably with attributes, you have > > to ensure that the length is > sizeof(ifinfomsg). >=20 > One of the many on-going efforts I have in progress is kernel side > filtering of route dumps. It has this same problem. For it I am > proposing a new flag: >=20 > #define RTM_F_PROPER_HEADER 0x4000 >=20 > ifinfomsg is 16 bytes which is > rtmsg at 12. If the message length is > > 16, then rtm_flags can be checked to know if the proper header is sent. >=20 > For ifaddrmsg things do not line up as well. Worst all of the flag bits > are used. But, perhaps one can be overloaded with the limit that you can > never filter on its presence. Since you are adding the first filter to > address dumps such a limitation should be ok. >=20 > For example something like this (whitespace damaged on paste) to remove > the guess work: >=20 > diff --git a/include/uapi/linux/if_addr.h b/include/uapi/linux/if_addr.h > index dfcf3ce0097f..8e3e9d475db5 100644 > --- a/include/uapi/linux/if_addr.h > +++ b/include/uapi/linux/if_addr.h > @@ -41,6 +41,11 @@ enum { > #define IFA_MAX (__IFA_MAX - 1) >=20 > /* ifa_flags */ > +/* IFA_F_PROPER_HEADER is only set in ifa_flags for dumps > + * to indicate the ancillary header is the expected ifaddrmsg > + * vs ifinfomsg from legacy userspace > + */ > +#define IFA_F_PROPER_HEADER 0x01 > #define IFA_F_SECONDARY 0x01 > #define IFA_F_TEMPORARY IFA_F_SECONDARY >=20 > diff --git a/net/ipv6/addrconf.c b/net/ipv6/addrconf.c > index bfe3ec7ecb14..256b9f88db8f 100644 > --- a/net/ipv6/addrconf.c > +++ b/net/ipv6/addrconf.c > @@ -5022,8 +5022,13 @@ static int inet6_dump_addr(struct sk_buff *skb, > struct netlink_callback *cb, > s_idx =3D idx =3D cb->args[1]; > s_ip_idx =3D ip_idx =3D cb->args[2]; >=20 > - if (nlmsg_parse(cb->nlh, sizeof(struct ifaddrmsg), tb, IFA_MAX, > - ifa_ipv6_policy, NULL) >=3D 0) { > + if (nlmsg_len(cb->nlh) >=3D sizeof(struct ifaddrmsg) && > + ((struct ifaddrmsg *) nlmsg_data(cb->nlh))->ifa_flags & > IFA_F_PROPER_HEADER) { > + > + if (nlmsg_parse(cb->nlh, sizeof(struct ifaddrmsg), tb, > IFA_MAX, > + ifa_ipv6_policy, NULL) >=3D 0) { > + ... > + > if (tb[IFA_TARGET_NETNSID]) { > netnsid =3D nla_get_s32(tb[IFA_TARGET_NETNSID]); >=20 >=20 > For ifaddrmsg ifa_flags aligns with ifi_type which is set by kernel side > so this should be ok. I like this idea way more than forcing userspace to create a nested attribute. If Jiri's question is answered can we get this patch on the road soon? Are you happy to send it on-top of net-next or do you want me to do it? Christian --rwtverreqta75zyj Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEE7btrcuORLb1XUhEwjrBW1T7ssS0FAlurViIACgkQjrBW1T7s sS3P3BAAq7ewyXVQbAc1d1MGZam4keEybTNeS3uI0gaD+zDQ9kOWuOs5PRaajOYf B8mYXqwV62Pqq2U70g9fD2KoH61vgPvpZlsis2ZNzs3xuGmetLw9EOWLYxv2o79W nsdISdSRLKDQ+gfnHJ/Vo6ijMKjQcGCZnvwKarce68w8i6bCQWbgZ1ktjuRC+r4v XxtQeOCk0numvryowuMyEEl2uetOB95DkrcB4jRMtYZBpwJSKCt8jjXpQPb4fvoh hMW8LMOCe8pTidqZWi+SBU6Y9GF6A3XmKxBpG2xmmu27OF7f6K6gPOyp5ogf/whH Ai9XJNfpIUObAmy0OFdmajGkNbxYPYXUb3yfhrCN7IzFuHcfaRdsoPtVp/nJtzV8 jnwqdZY7Eq4fR23aPhN+w5EYswc//rzAULycwKVsEWWKO/3oAyd8VUP7WkJmpaEq pBKV/PStmMDS0FluDKsdaPwbWi8UZ5LR4F2w1lUG7YinfP3xm7Nkx+swQeruFIVd A12Bg4J1EhKxbicJjWNhnIWuJjvilM21Qn/nb/JIohRLx5LoSwn9AnMwzA4nbDvw 7kJlqr2o/k0bQHp9RrkMSANvzSdxaApBrR8KLbopE+XrjshUvKmUTZ+FcuSv9GMS aExpbBs1mC11rLwFhBTrlQp/HI71i3Zf5BvkSMTgCk6XWIrzaF8= =mwwD -----END PGP SIGNATURE----- --rwtverreqta75zyj--