From nobody Sat Mar 7 07:51:25 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4fSb7f36gmz6T33Z for ; Sat, 07 Mar 2026 07:51:26 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R12" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4fSb7d6Zpxz3dNZ for ; Sat, 07 Mar 2026 07:51:25 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1772869885; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=jluR2oCqBGKXOkKewvWvMNMZD9A3ekkqpWJ7f3wwNjc=; b=xC/DyLUqM5NM3ez4GfPCvsssVBwSKy7XSLA5uLmSYARX86fuabTYWHQayfljTaLD8u3JOM MaxOneOCF0mBaEob/ZW5HbMOiMbLK/jaLVl5u/b9Ze3DlvPEijvTTlrGk+rXEvxzDRRkai nAPrtQRg1i7DMy4122tF0S2bxraPkkuXAbn8R+van0YjaweQ7GbEjoGXYlWnAfitpyx/U5 4fSOlGpFDIUoJwKg+RdQvZy45FSwnQh2tLIpvWZ4gAc4/lNyfRll1JcZ1igOM4WvRkSREL AzY/NjYROh6UilBs9n5SbkyoDZQNX6xBxi2FwBU+sGuzc+3AQFvwOZjlN1SyAQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1772869885; a=rsa-sha256; cv=none; b=JlWgLr6jGZvY1y1wCKJJtEwRZCfCCtMAL8DOoE23J3QMH4Rtbx7Fsu7LjGIfAcNDilgxIg zhACLcBOqSzkuVd5y3nlIioP3ts9GmsKG9WXQkUPdIDIz7MZe+l6v4y0QPhzDA9BYhB9mF 3mUTlXtuqJf/IJNE3so3UE+nOvAzUm7kPNGnNbKuRsblTSjx6ih0buZZi3j3u+cvkGbrQg xxn+XIDneebZV7SMZM5x99+zxuOfUqO/QWisTFsVfpBYbInwZv+qAqlBWOJPlWc6nAirTO dXS+xbalBWXf0zfbK7ESBXnvN+WlUjM72hoqrbhKXIBmaIzf1AzLAcAlgegE4g== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1772869885; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=jluR2oCqBGKXOkKewvWvMNMZD9A3ekkqpWJ7f3wwNjc=; b=Xq5mmg6Yq/QwUQd9SxJIKQexx5hI1yzkvmqwzHBVaS9xAAX4PWLh/rjs8EnLeU1+C0nn5A Dst/6FrQlVhNt8JhIHn3gTs3R6E6h54Nt47EvV3U/f9HTA+0KU2NSJgX7eaNLZHtuQBHIf 9IWlXMqkfgD7ijRJZv+6htfRNjNeauD7QT5XJNXoH4j7iN61zmTRTK8hrcIgpYq8iRVU/s dNGQLsdOxTzqxvpaBzFxRqgAdReqEvC290EASL26dRkTFhS0RPBWSlDSZSqcSsm2ma14Cb zEuAE/7BD6bI+uBYTveA+pGvvxsF1pyRYhr5ba5yVmu59umelueexOcHj9hukg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4fSb7d603Jz10tq for ; Sat, 07 Mar 2026 07:51:25 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 23d45 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Sat, 07 Mar 2026 07:51:25 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org Cc: NVSRahul From: Warner Losh Subject: git: 3570b19d74ed - main - uuidgen: generate UUIDs in bounded batches to respect kernel limit List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: imp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 3570b19d74ed94dcc7fac2dcbf42b378bc72362e Auto-Submitted: auto-generated Date: Sat, 07 Mar 2026 07:51:25 +0000 Message-Id: <69abd8fd.23d45.61b5f1e@gitrepo.freebsd.org> The branch main has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=3570b19d74ed94dcc7fac2dcbf42b378bc72362e commit 3570b19d74ed94dcc7fac2dcbf42b378bc72362e Author: NVSRahul AuthorDate: 2026-01-13 05:52:06 +0000 Commit: Warner Losh CommitDate: 2026-03-07 07:51:16 +0000 uuidgen: generate UUIDs in bounded batches to respect kernel limit The uuidgen(2) system call enforces a hard upper limit of 2048 UUIDs per invocation. uuidgen(1) previously attempted to generate arbitrary counts in a single call and allocated memory accordingly, leading to EINVAL errors, unnecessary memory usage, and potential overflow risks. Generate UUIDs in fixed-size batches, streaming output incrementally while preserving existing semantics. Mirror the kernel limit explicitly since it is not exposed via a public interface. Signed-off-by: NVSRahul Reviewed by: imp Pull Request: https://github.com/freebsd/freebsd-src/pull/1965 --- bin/uuidgen/uuidgen.1 | 4 --- bin/uuidgen/uuidgen.c | 85 ++++++++++++++++++++++++++++----------------------- sys/kern/kern_uuid.c | 2 +- sys/sys/uuid.h | 6 ++++ 4 files changed, 53 insertions(+), 44 deletions(-) diff --git a/bin/uuidgen/uuidgen.1 b/bin/uuidgen/uuidgen.1 index f7911b408f36..f88ea8d8631d 100644 --- a/bin/uuidgen/uuidgen.1 +++ b/bin/uuidgen/uuidgen.1 @@ -56,10 +56,6 @@ This option controls creation of compact UUID (without hyphen). .It Fl n This option controls the number of identifiers generated. By default, multiple identifiers are generated in batch. -The upper hard limit is 2048 -.Po see -.Xr uuidgen 2 -.Pc . .It Fl o Redirect output to .Ar filename diff --git a/bin/uuidgen/uuidgen.c b/bin/uuidgen/uuidgen.c index 3e2c0324a959..bcb7a45b665d 100644 --- a/bin/uuidgen/uuidgen.c +++ b/bin/uuidgen/uuidgen.c @@ -26,7 +26,9 @@ * */ +#include #include +#include #include #include @@ -156,46 +158,51 @@ main(int argc, char *argv[]) if (count == -1) count = 1; - store = (uuid_t *)malloc(sizeof(uuid_t) * count); - if (store == NULL) - err(1, "malloc()"); - - if (!iterate) { - /* Get them all in a single batch */ - if (version == 1) { - if (uuidgen(store, count) != 0) - err(1, "uuidgen()"); - } else if (version == 4) { - if (uuidgen_v4(store, count) != 0) - err(1, "uuidgen_v4()"); - } else { - err(1, "unsupported version"); - } - } else { - uuid = store; - for (i = 0; i < count; i++) { - if (version == 1) { - if (uuidgen(uuid++, 1) != 0) - err(1, "uuidgen()"); - } else if (version == 4) { - if (uuidgen_v4(uuid++, 1) != 0) - err(1, "uuidgen_v4()"); - } else { - err(1, "unsupported version"); - } - } - } - - uuid = store; - while (count--) { - tostring(uuid++, &p, &status); - if (status != uuid_s_ok) - err(1, "cannot stringify a UUID"); - fprintf(fp, "%s\n", p); - free(p); - } + store = calloc(MIN(count, UUIDGEN_BATCH_MAX), sizeof(uuid_t)); + if (store == NULL) + err(1, "calloc()"); + + while (count > 0) { + int batch = (count > UUIDGEN_BATCH_MAX) ? UUIDGEN_BATCH_MAX : count; + + if (!iterate) { + if (version == 1) { + if (uuidgen(store, batch) != 0) + err(1, "uuidgen()"); + } else if (version == 4) { + if (uuidgen_v4(store, batch) != 0) + err(1, "uuidgen_v4()"); + } else { + err(1, "unsupported version"); + } + } else { + uuid = store; + for (i = 0; i < batch; i++) { + if (version == 1) { + if (uuidgen(uuid++, 1) != 0) + err(1, "uuidgen()"); + } else if (version == 4) { + if (uuidgen_v4(uuid++, 1) != 0) + err(1, "uuidgen_v4()"); + } else { + err(1, "unsupported version"); + } + } + } + + uuid = store; + for (i = 0; i < batch; i++) { + tostring(uuid++, &p, &status); + if (status != uuid_s_ok) + err(1, "cannot stringify a UUID"); + fprintf(fp, "%s\n", p); + free(p); + } + + count -= batch; + } + free(store); - free(store); if (fp != stdout) fclose(fp); return (0); diff --git a/sys/kern/kern_uuid.c b/sys/kern/kern_uuid.c index fb27f7bbf736..136242ca2087 100644 --- a/sys/kern/kern_uuid.c +++ b/sys/kern/kern_uuid.c @@ -186,7 +186,7 @@ sys_uuidgen(struct thread *td, struct uuidgen_args *uap) * like to have some sort of upper-bound that's less than 2G :-) * XXX probably needs to be tunable. */ - if (uap->count < 1 || uap->count > 2048) + if (uap->count < 1 || uap->count > UUIDGEN_BATCH_MAX) return (EINVAL); count = uap->count; diff --git a/sys/sys/uuid.h b/sys/sys/uuid.h index 5fc5e61457de..3694988ccf5c 100644 --- a/sys/sys/uuid.h +++ b/sys/sys/uuid.h @@ -34,6 +34,12 @@ /* Length of a node address (an IEEE 802 address). */ #define _UUID_NODE_LEN 6 +/* + * The kernel imposes a limit on the number of UUIDs generated per call + * to avoid unbounded allocations. + */ +#define UUIDGEN_BATCH_MAX 2048 + /* * See also: * http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt