Re: git: eb8dcdeac22d - main - jail: network epoch protection for IP address lists

From: Gleb Smirnoff <glebius_at_freebsd.org>
Date: Sun, 26 Dec 2021 19:27:57 UTC
On Sun, Dec 26, 2021 at 07:15:58PM +0000, Jessica Clarke wrote:
J> > On Sun, Dec 26, 2021 at 07:07:06PM +0000, Jessica Clarke wrote:
J> > J> > +struct prison_ip {
J> > J> > +	struct epoch_context ctx;
J> > J> > +	uint32_t	ips;
J> > J> > +#ifdef FUTURE_C
J> > J> > +	union {
J> > J> > +		struct in_addr pr_ip4[];
J> > J> > +		struct in6_addr pr_ip6[];
J> > J> > +	};
J> > J> > +#else /* No future C :( */
J> > J> > +#define	PR_IP(pip, i)	((const char *)((pip) + 1) + pr_families[af].size * (i))
J> > J> > +#define	PR_IPD(pip, i)	((char *)((pip) + 1) + pr_families[af].size * (i))
J> > J> > +#endif
J> > J> > +};
J> > J> 
J> > J> You can make this work with a prison_ip base and prison_ipv[46] derived
J> > J> structs.
J> > J> 
J> > J> As it stands this is quite gross, you’re assuming things about
J> > J> alignment, and don’t even have a flexible char[] at the end to
J> > J> represent the extra data.
J> > 
J> > Will adding char [] to the end be sufficient to guarantee proper alignment?
J> 
J> No.
J> 
J> > Using prison_ipv[46] derived structs won't work as most functions are now
J> > made protocol independent.
J> 
J> Why not? Just cast from prison_ip * to prison_ipv4 * when known/needed?

What about this?

diff --git a/sys/kern/kern_jail.c b/sys/kern/kern_jail.c
index f1c81d8813bd..ee8bfadb4a3c 100644
--- a/sys/kern/kern_jail.c
+++ b/sys/kern/kern_jail.c
@@ -572,11 +572,15 @@ struct prison_ip {
                struct in_addr pr_ip4[];
                struct in6_addr pr_ip6[];
        };
+};
 #else /* No future C :( */
+       struct in_addr pr_ip4[];
 #define        PR_IP(pip, i)   ((const char *)((pip) + 1) + pr_families[af].size * (i))
 #define        PR_IPD(pip, i)  ((char *)((pip) + 1) + pr_families[af].size * (i))
-#endif
 };
+_Static_assert(__alignof(struct in_addr) == __alignof(struct in6_addr),
+    "AF_INET and AF_INET6 address alignment shall be the same");
+#endif
 
-- 
Gleb Smirnoff