From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 025F65A9D0A for ; Mon, 6 May 2024 09:22:30 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1714980145; bh=2+8y3cOXxfDYW6TAZpALNz6I/aJlAi2J0rxahXQDrkM=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=ny/SKLta0/oPsLfEISkzmTE0eJaA9FS3aHdVIO3YgOci6jz4Xu6QhTTcNen50XAVC kRt8fJqSYdl5qidtb/LipevAEZe9Ah+OuiSt8o4fYCyWcAzKotAoHpV2wn6dY9D5wS 5BmQHzoWPpjsWkeCZlIo9aqqKhTU2ujr2CgVHx4TGr3x93P1yC2wS3fk0UP/rIypO1 AU54FCefGFEM85emZSRKkCG8vR58afBmWfrOnR1ajViwg/24/fW1o3VhtQ+9+wi+I5 EF+N8soDi4zl3FffDbK+lbrj72GMakDYu7obUya9IXRkA7dbOoaVKtUO54QQtRRaAy 9HD/MHkLFcZHQ== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4VXtCP5NLxz4wcR; Mon, 6 May 2024 17:22:25 +1000 (AEST) Date: Mon, 6 May 2024 17:22:20 +1000 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH v2] netlink: Don't duplicate routes referring to unrelated host interfaces Message-ID: References: <20240502061716.4164409-1-sbrivio@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="VF5y82mR42Q3xgQF" Content-Disposition: inline In-Reply-To: <20240502061716.4164409-1-sbrivio@redhat.com> Message-ID-Hash: YHHPGUINNNADKJZTUWVR46L6QBALK355 X-Message-ID-Hash: YHHPGUINNNADKJZTUWVR46L6QBALK355 X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top, runsisi X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: --VF5y82mR42Q3xgQF Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, May 02, 2024 at 08:17:16AM +0200, Stefano Brivio wrote: > We take care of this in nl_addr_dup(): if the interface index > associated to an address doesn't match the selected host interface > (ifa->ifa_index !=3D ifi_src), we don't copy that address. >=20 > But for routes, we just unconditionally update the interface index to > match the index in the target namespace, even if the source interface > didn't match. >=20 > This might happen in two cases: with a pre-4.20 kernel without support > for NETLINK_GET_STRICT_CHK, which won't filter routes based on the > interface we pass in the request, as reported by runsisi, and any > kernel with support for multipath routes where any of the nexthops > refers to an unrelated host interface. >=20 > In both cases, check the index of the source interface, and avoid > copying unrelated routes. >=20 > Reported-by: runsisi > Link: https://bugs.passt.top/show_bug.cgi?id=3D86 > Signed-off-by: Stefano Brivio Reviewed-by: David Gibson > --- > v2: Set NLMSG_NOOP on the message instead of AF_UNSPEC on the route > to discard it, to keep the check on insertion simple. >=20 > netlink.c | 39 +++++++++++++++++++++++++++++++++------ > 1 file changed, 33 insertions(+), 6 deletions(-) >=20 > diff --git a/netlink.c b/netlink.c > index cdfe68c..b1c0cce 100644 > --- a/netlink.c > +++ b/netlink.c > @@ -554,21 +554,32 @@ int nl_route_dup(int s_src, unsigned int ifi_src, > NLMSG_OK(nh, left) && (status =3D nl_status(nh, left, seq)) > 0; > nh =3D NLMSG_NEXT(nh, left)) { > struct rtmsg *rtm =3D (struct rtmsg *)NLMSG_DATA(nh); > + bool discard =3D false; > struct rtattr *rta; > size_t na; > =20 > if (nh->nlmsg_type !=3D RTM_NEWROUTE) > continue; > =20 > - dup_routes++; > - > for (rta =3D RTM_RTA(rtm), na =3D RTM_PAYLOAD(nh); RTA_OK(rta, na); > rta =3D RTA_NEXT(rta, na)) { > /* RTA_OIF and RTA_MULTIPATH attributes carry the > - * identifier of a host interface. Change them to match > - * the corresponding identifier in the target namespace. > - */ > + * identifier of a host interface. If they match the > + * host interface we're copying from, change them to > + * match the corresponding identifier in the target > + * namespace. > + * > + * If RTA_OIF doesn't match (NETLINK_GET_STRICT_CHK not > + * available), or if any interface index in nexthop > + * objects differ from the host interface, discard the > + * route altogether. > + */ > if (rta->rta_type =3D=3D RTA_OIF) { > + if (*(unsigned int *)RTA_DATA(rta) !=3D ifi_src) { > + discard =3D true; > + break; > + } > + > *(unsigned int *)RTA_DATA(rta) =3D ifi_dst; > } else if (rta->rta_type =3D=3D RTA_MULTIPATH) { > size_t nh_len =3D RTA_PAYLOAD(rta); > @@ -576,8 +587,19 @@ int nl_route_dup(int s_src, unsigned int ifi_src, > =20 > for (rtnh =3D (struct rtnexthop *)RTA_DATA(rta); > RTNH_OK(rtnh, nh_len); > - rtnh =3D RTNH_NEXT_AND_DEC(rtnh, nh_len)) > + rtnh =3D RTNH_NEXT_AND_DEC(rtnh, nh_len)) { > + int src =3D (int)ifi_src; > + > + if (rtnh->rtnh_ifindex !=3D src) { > + discard =3D true; > + break; > + } > + > rtnh->rtnh_ifindex =3D ifi_dst; > + } > + > + if (discard) > + break; > } else if (rta->rta_type =3D=3D RTA_PREFSRC) { > /* Host routes might include a preferred source > * address, which must be one of the host's > @@ -588,6 +610,11 @@ int nl_route_dup(int s_src, unsigned int ifi_src, > rta->rta_type =3D RTA_UNSPEC; > } > } > + > + if (discard) > + nh->nlmsg_type =3D NLMSG_NOOP; > + else > + dup_routes++; > } > =20 > if (!NLMSG_OK(nh, left)) { --=20 David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson --VF5y82mR42Q3xgQF Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmY4hSwACgkQzQJF27ox 2Gez7Q/+PgwK92LrcBMOSoV42T5ij3I4thZSTHFxQtBq3ob0PZ/EJUESlSvjdL8U 5O3PqvPQehZxdqPmSUeu+k7G0k06UPmUYNcFwpzECcUdkwBgENC6vzlibhXP4k9W GNaJD8ttv4KE5dt5qOt1HUWKyXJJFYej8GWPJrvqKVzfhzHW08KQqh+HeiF2XWEj Szb3y31b6LuST2/9Ocx2PfmuBOZpZsJPJXGCbOm5I61kzN538HRPMi4v7xVw+M5V xu/PZMQVInFzNAcHpw1ijpnk558OadpdXI038cXuchelrnK6viM3S2ZuWV+Q8v// dQbgUjph3QV8RpEWWNvACdQJRmHTxoseyBr59FTvtSt+y3yJPJD2PAmkuNWbazqN VfKT5RNelu/nDsht7616gO7GxIO0gRChyaFsM2QW1v5+2ociYeF0AewyAE4+Eitw ED2UUBgEIITMOSN2y3AcZKTX5f3F9qw9EuyUEKmclc+XNp4uG+FTtOOOnUz1W44/ cWb9KqHUehG85fJsB8DwLsiBlrHCnK9zOGHIMlD6isC9ryhB4YH4Sjq6l/Zmj12/ Zm2hYsGXHdVDe/Nw0fJWbRmr1Zjhp2tGly0hRh/DzChFtJBU4JWKz/30r04L+76V 6oWMIq5IXoHKO1SJFQCfkbPFyhAF/yNhhIXeKUcnu0HY+kE73+8= =6iZL -----END PGP SIGNATURE----- --VF5y82mR42Q3xgQF--