Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 10 Dec 2025 10:58:05 +0000
From:      Brooks Davis <brooks@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: ac79e2e025e0 - main - get*ent: be consistant about _ALIGN(p) - p
Message-ID:  <6939523d.8560.71c378b7@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help

The branch main has been updated by brooks:

URL: https://cgit.FreeBSD.org/src/commit/?id=ac79e2e025e03b7038e3abc886e34a03f5ec2934

commit ac79e2e025e03b7038e3abc886e34a03f5ec2934
Author:     Brooks Davis <brooks@FreeBSD.org>
AuthorDate: 2025-12-10 10:57:34 +0000
Commit:     Brooks Davis <brooks@FreeBSD.org>
CommitDate: 2025-12-10 10:57:34 +0000

    get*ent: be consistant about _ALIGN(p) - p
    
    Add an nscache specific inline function to calculate the misalignment
    rather than adding and subtracting _ALIGN(p) and p which can take the
    buffer far out of bound (undefined behavior in C and unsupported on
    CHERI).
    
    Reviewed by:    kib
    Effort:         CHERI upstreaming
    Obtained from:  CheriBSD
    Sponsored by:   DARPA
    Differential Revision:  https://reviews.freebsd.org/D53945
---
 lib/libc/gen/getgrent.c      | 6 +++---
 lib/libc/include/nscache.h   | 8 ++++++++
 lib/libc/net/gethostnamadr.c | 4 ++--
 lib/libc/net/getnetnamadr.c  | 4 ++--
 lib/libc/net/getprotoent.c   | 4 ++--
 lib/libc/net/getservent.c    | 4 ++--
 lib/libc/rpc/getrpcent.c     | 4 ++--
 7 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/lib/libc/gen/getgrent.c b/lib/libc/gen/getgrent.c
index 508e3d63eb60..8819708556b9 100644
--- a/lib/libc/gen/getgrent.c
+++ b/lib/libc/gen/getgrent.c
@@ -347,16 +347,16 @@ grp_unmarshal_func(char *buffer, size_t buffer_size, void *retval, va_list ap,
 	memcpy(&p, buffer + sizeof(struct group), sizeof(char *));
 
 	if (orig_buf_size + sizeof(struct group) + sizeof(char *) +
-	    _ALIGN(p) - (size_t)p < buffer_size) {
+	    __nss_buf_misalignment(p) < buffer_size) {
 		*ret_errno = ERANGE;
 		return (NS_RETURN);
 	}
 
 	orig_buf = (char *)_ALIGN(orig_buf);
 	memcpy(orig_buf, buffer + sizeof(struct group) + sizeof(char *) +
-	    _ALIGN(p) - (size_t)p,
+	    __nss_buf_misalignment(p),
 	    buffer_size - sizeof(struct group) - sizeof(char *) -
-	    _ALIGN(p) + (size_t)p);
+	    __nss_buf_misalignment(p));
 	p = (char *)_ALIGN(p);
 
 	NS_APPLY_OFFSET(grp->gr_name, orig_buf, p, char *);
diff --git a/lib/libc/include/nscache.h b/lib/libc/include/nscache.h
index aab29e411ddc..5932d103a4da 100644
--- a/lib/libc/include/nscache.h
+++ b/lib/libc/include/nscache.h
@@ -29,6 +29,8 @@
 #ifndef __NS_CACHE_H__
 #define __NS_CACHE_H__
 
+#include <sys/_align.h>
+
 #include "nscachedcli.h"
 
 typedef int (*nss_cache_id_func_t)(char *, size_t *, va_list, void *);
@@ -178,6 +180,12 @@ typedef struct _nss_cache_data {
 } nss_cache_data;
 
 __BEGIN_DECLS
+static inline __ptrdiff_t
+__nss_buf_misalignment(const void *p)
+{
+	return ((char *)_ALIGN(p) - (char *)p);
+}
+
 /* dummy function, which is needed to make nss_method_lookup happy */
 extern	int	__nss_cache_handler(void *, void *, va_list);
 
diff --git a/lib/libc/net/gethostnamadr.c b/lib/libc/net/gethostnamadr.c
index b54ca8b2998e..6a8648ac693d 100644
--- a/lib/libc/net/gethostnamadr.c
+++ b/lib/libc/net/gethostnamadr.c
@@ -402,9 +402,9 @@ host_unmarshal_func(char *buffer, size_t buffer_size, void *retval, va_list ap,
 
 	orig_buf = (char *)_ALIGN(orig_buf);
 	memcpy(orig_buf, buffer + sizeof(struct hostent) + sizeof(char *) +
-	    _ALIGN(p) - (size_t)p,
+	    __nss_buf_misalignment(p),
 	    buffer_size - sizeof(struct hostent) - sizeof(char *) -
-	    _ALIGN(p) + (size_t)p);
+	    __nss_buf_misalignment(p));
 	p = (char *)_ALIGN(p);
 
 	NS_APPLY_OFFSET(ht->h_name, orig_buf, p, char *);
diff --git a/lib/libc/net/getnetnamadr.c b/lib/libc/net/getnetnamadr.c
index 660de3302606..b380912955e8 100644
--- a/lib/libc/net/getnetnamadr.c
+++ b/lib/libc/net/getnetnamadr.c
@@ -249,9 +249,9 @@ net_unmarshal_func(char *buffer, size_t buffer_size, void *retval, va_list ap,
 
 	orig_buf = (char *)_ALIGN(orig_buf);
 	memcpy(orig_buf, buffer + sizeof(struct netent) + sizeof(char *) +
-	    _ALIGN(p) - (size_t)p,
+	    __nss_buf_misalignment(p),
 	    buffer_size - sizeof(struct netent) - sizeof(char *) -
-	    _ALIGN(p) + (size_t)p);
+	    __nss_buf_misalignment(p));
 	p = (char *)_ALIGN(p);
 
 	NS_APPLY_OFFSET(ne->n_name, orig_buf, p, char *);
diff --git a/lib/libc/net/getprotoent.c b/lib/libc/net/getprotoent.c
index 9fcbf41530cf..5f25333caff3 100644
--- a/lib/libc/net/getprotoent.c
+++ b/lib/libc/net/getprotoent.c
@@ -265,9 +265,9 @@ __proto_unmarshal_func(char *buffer, size_t buffer_size, void *retval,
 
 	orig_buf = (char *)_ALIGN(orig_buf);
 	memcpy(orig_buf, buffer + sizeof(struct protoent) + sizeof(char *) +
-	    _ALIGN(p) - (size_t)p,
+	    __nss_buf_misalignment(p),
 	    buffer_size - sizeof(struct protoent) - sizeof(char *) -
-	    _ALIGN(p) + (size_t)p);
+	    __nss_buf_misalignment(p));
 	p = (char *)_ALIGN(p);
 
 	NS_APPLY_OFFSET(proto->p_name, orig_buf, p, char *);
diff --git a/lib/libc/net/getservent.c b/lib/libc/net/getservent.c
index 26f68f24cc78..1cf7921ac679 100644
--- a/lib/libc/net/getservent.c
+++ b/lib/libc/net/getservent.c
@@ -1084,9 +1084,9 @@ serv_unmarshal_func(char *buffer, size_t buffer_size, void *retval, va_list ap,
 
 	orig_buf = (char *)_ALIGN(orig_buf);
 	memcpy(orig_buf, buffer + sizeof(struct servent) + sizeof(char *) +
-	    (_ALIGN(p) - (size_t)p),
+	    __nss_buf_misalignment(p),
 	    buffer_size - sizeof(struct servent) - sizeof(char *) -
-	    (_ALIGN(p) - (size_t)p));
+	    __nss_buf_misalignment(p));
 	p = (char *)_ALIGN(p);
 
 	NS_APPLY_OFFSET(serv->s_name, orig_buf, p, char *);
diff --git a/lib/libc/rpc/getrpcent.c b/lib/libc/rpc/getrpcent.c
index ee36c11b303f..a6eaac7079a3 100644
--- a/lib/libc/rpc/getrpcent.c
+++ b/lib/libc/rpc/getrpcent.c
@@ -778,9 +778,9 @@ rpc_unmarshal_func(char *buffer, size_t buffer_size, void *retval, va_list ap,
 
 	orig_buf = (char *)_ALIGN(orig_buf);
 	memcpy(orig_buf, buffer + sizeof(struct rpcent) + sizeof(char *) +
-	    _ALIGN(p) - (size_t)p,
+	    __nss_buf_misalignment(p),
 	    buffer_size - sizeof(struct rpcent) - sizeof(char *) -
-	    _ALIGN(p) + (size_t)p);
+	    __nss_buf_misalignment(p));
 	p = (char *)_ALIGN(p);
 
 	NS_APPLY_OFFSET(rpc->r_name, orig_buf, p, char *);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?6939523d.8560.71c378b7>