git: d11904b35021 - main - Fix lib/libc/nss/getgr_test with large numbers of groups

From: Alan Somers <asomers_at_FreeBSD.org>
Date: Wed, 01 Jan 2025 20:24:41 UTC
The branch main has been updated by asomers:

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

commit d11904b350214943dedb64c7121d4602799d7afd
Author:     Alan Somers <asomers@FreeBSD.org>
AuthorDate: 2024-12-31 20:41:01 +0000
Commit:     Alan Somers <asomers@FreeBSD.org>
CommitDate: 2025-01-01 20:23:47 +0000

    Fix lib/libc/nss/getgr_test with large numbers of groups
    
    These tests create a linked list with one entry for every group on the
    running system.  On a system with about 30,000 groups, the test took 69
    seconds to run, and crashed Kyua with the below error:
    
    kyua: E: string or blob too big (sqlite op: sqlite3_bind_blob) (sqlite db: /root/.kyua/store/results.usr_tests.20241231-203317-570235.db).
    
    Fix the test by limiting it to operating on the first 1024 groups.
    Apply the same change to getpw_test and getserv_test too, which are
    vulnerable to the same problem.
    
    MFC after:      2 weeks
    Sponsored by:   ConnectWise
    Reviewed by:    markj
    Differential Revision: https://reviews.freebsd.org/D48275
---
 lib/libc/tests/nss/getgr_test.c   | 4 ++++
 lib/libc/tests/nss/getpw_test.c   | 4 ++++
 lib/libc/tests/nss/getserv_test.c | 4 ++++
 3 files changed, 12 insertions(+)

diff --git a/lib/libc/tests/nss/getgr_test.c b/lib/libc/tests/nss/getgr_test.c
index 7c0e265fa6f6..974632d4b7c7 100644
--- a/lib/libc/tests/nss/getgr_test.c
+++ b/lib/libc/tests/nss/getgr_test.c
@@ -293,6 +293,8 @@ group_fill_test_data(struct group_test_data *td,
     int (*cb)(struct group *, void *))
 {
 	struct group *grp;
+	const int limit = 1024;
+	int count = 0;
 
 	setgroupent(1);
 	while ((grp = getgrent()) != NULL) {
@@ -303,6 +305,8 @@ group_fill_test_data(struct group_test_data *td,
 		} else {
 			return (-1);
 		}
+		if (++count >= limit)
+			break;
 	}
 	endgrent();
 
diff --git a/lib/libc/tests/nss/getpw_test.c b/lib/libc/tests/nss/getpw_test.c
index 3a44497cf848..434d86a31591 100644
--- a/lib/libc/tests/nss/getpw_test.c
+++ b/lib/libc/tests/nss/getpw_test.c
@@ -240,6 +240,8 @@ passwd_fill_test_data(struct passwd_test_data *td,
     int (*cb)(struct passwd *, void *))
 {
 	struct passwd *pwd;
+	const int limit = 1024;
+	int count = 0;
 
 	setpassent(1);
 	while ((pwd = getpwent()) != NULL) {
@@ -250,6 +252,8 @@ passwd_fill_test_data(struct passwd_test_data *td,
 		} else {
 			return (-1);
 		}
+		if (++count >= limit)
+			break;
 	}
 	endpwent();
 
diff --git a/lib/libc/tests/nss/getserv_test.c b/lib/libc/tests/nss/getserv_test.c
index 486a8c20836f..cc66fdb2fa52 100644
--- a/lib/libc/tests/nss/getserv_test.c
+++ b/lib/libc/tests/nss/getserv_test.c
@@ -283,6 +283,8 @@ static int
 servent_fill_test_data(struct servent_test_data *td)
 {
 	struct servent *serv;
+	const int limit = 1024;
+	int count = 0;
 
 	setservent(1);
 	while ((serv = getservent()) != NULL) {
@@ -290,6 +292,8 @@ servent_fill_test_data(struct servent_test_data *td)
 			TEST_DATA_APPEND(servent, td, serv);
 		else
 			return (-1);
+		if (++count >= limit)
+			break;
 	}
 	endservent();