git: 0d8a925984d7 - stable/15 - su, login: fix heap over-read in environment export filter

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Mon, 16 Nov 2026 15:17:46 UTC
The branch stable/15 has been updated by des:

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

commit 0d8a925984d70b8a3e2e1f8cd722afee2f967fe1
Author:     Osvaldo Janeri Filho <ojanerif@amd.com>
AuthorDate: 2026-11-12 14:23:56 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-11-16 14:52:03 +0000

    su, login: fix heap over-read in environment export filter
    
    ok_to_export() (su.c) and export() (login.c) read s[n] before
    strncmp(s, *pp, n) confirms s is at least n bytes long, where n is
    the length of the blocked variable name. A PAM environment entry
    shorter than that name over-reads past its heap allocation.
    
    Check strncmp() first; it stops at s's NUL terminator and never
    reads past the boundary that guarded the s[n] access.
    
    MFC after:      1 week
    Sponsored by:   Advanced Micro Devices, Inc.
    Signed-off-by:  Osvaldo Janeri Filho <ojanerif@amd.com>
    Reviewed by:    des, markj
    Differential Revision:  https://reviews.freebsd.org/D61420
    
    (cherry picked from commit e1b5e1f9ddac43ed669a44cfdbba1b394d144c6b)
---
 usr.bin/login/login.c | 2 +-
 usr.bin/su/su.c       | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/usr.bin/login/login.c b/usr.bin/login/login.c
index 6a0c6a5b6bbc..e5a40b819833 100644
--- a/usr.bin/login/login.c
+++ b/usr.bin/login/login.c
@@ -797,7 +797,7 @@ export(const char *s)
 		return (0);
 	for (pp = noexport; *pp != NULL; pp++) {
 		n = strlen(*pp);
-		if (s[n] == '=' && strncmp(s, *pp, n) == 0)
+		if (strncmp(s, *pp, n) == 0 && s[n] == '=')
 			return (0);
 	}
 	*p = '\0';
diff --git a/usr.bin/su/su.c b/usr.bin/su/su.c
index 312a248b11f5..daf20c85d471 100644
--- a/usr.bin/su/su.c
+++ b/usr.bin/su/su.c
@@ -586,7 +586,7 @@ ok_to_export(const char *s)
 		return 0;
 	for (pp = noexport; *pp != NULL; pp++) {
 		n = strlen(*pp);
-		if (s[n] == '=' && strncmp(s, *pp, n) == 0)
+		if (strncmp(s, *pp, n) == 0 && s[n] == '=')
 			return 0;
 	}
 	return 1;