From nobody Tue Jun 23 21:22:55 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 99A83C433F5 for ; Fri, 25 Feb 2022 13:37:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241525AbiBYNiR (ORCPT ); Fri, 25 Feb 2022 08:38:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41924 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241229AbiBYNiO (ORCPT ); Fri, 25 Feb 2022 08:38:14 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52E921688FE for ; Fri, 25 Feb 2022 05:37:42 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 13161B83022 for ; Fri, 25 Feb 2022 13:37:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6D234C340E7; Fri, 25 Feb 2022 13:37:39 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="FsPQphjf" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1645796257; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iLIf2o0dj/gBCR4aqE4HcDV1JjQTn81CJYVohfhg7z8=; b=FsPQphjfbMLgAtdVrMWrs3R0Tq0dV+ZHbP2+Z8PsCghZ/vAr7/DY9WKyaVnnbbCvU7PCNG RZoFMqQs/Rr+rRR5Osu+vLJP1cQjuNl/kVcUqFmfcvanYLCIepk7aU7+x5UCPJTL8XxXH3 Re0FIwv2lzkRYHKIdu71Zz/j6Pk4ZmQ= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 5ea695b1 (TLSv1.3:AEAD-AES256-GCM-SHA384:256:NO); Fri, 25 Feb 2022 13:37:37 +0000 (UTC) From: "Jason A. Donenfeld" To: linux-kernel@vger.kernel.org Cc: "Jason A. Donenfeld" , Dominik Brodowski Subject: [PATCH v2] random: cleanup UUID handling Date: Fri, 25 Feb 2022 14:37:32 +0100 Message-Id: <20220225133732.923604-1-Jason@zx2c4.com> In-Reply-To: <20220224220958.93263-1-Jason@zx2c4.com> References: <20220224220958.93263-1-Jason@zx2c4.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Rather than hard coding various lengths, we can use the right constants. Strings should be `char *` while buffers should be `u8 *`. Rather than have a nonsensical and unused maxlength, just remove it. Finally, use snprintf instead of sprintf, just out of good hygiene. As well, remove the old comment about returning a binary UUID via the binary sysctl syscall. That syscall was removed from the kernel in 5.5, and actually, the "uuid_strategy" function and related infrastructure for even serving it via the binary sysctl syscall was removed with 894d2491153a ("sysctl drivers: Remove dead binary sysctl support") back in 2.6.33. Reviewed-by: Dominik Brodowski Signed-off-by: Jason A. Donenfeld --- drivers/char/random.c | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/drivers/char/random.c b/drivers/char/random.c index 19bf44b9ba0f..ef138ceec241 100644 --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -1634,22 +1634,25 @@ const struct file_operations urandom_fops =3D { static int sysctl_random_min_urandom_seed =3D 60; static int sysctl_random_write_wakeup_bits =3D POOL_MIN_BITS; static int sysctl_poolsize =3D POOL_BITS; -static char sysctl_bootid[16]; +static u8 sysctl_bootid[UUID_SIZE]; =20 /* * This function is used to return both the bootid UUID, and random - * UUID. The difference is in whether table->data is NULL; if it is, + * UUID. The difference is in whether table->data is NULL; if it is, * then a new UUID is generated and returned to the user. - * - * If the user accesses this via the proc interface, the UUID will be - * returned as an ASCII string in the standard UUID format; if via the - * sysctl system call, as 16 bytes of binary data. */ static int proc_do_uuid(struct ctl_table *table, int write, void *buffer, size_t *lenp, loff_t *ppos) { - struct ctl_table fake_table; - unsigned char buf[64], tmp_uuid[16], *uuid; + u8 tmp_uuid[UUID_SIZE], *uuid; + char uuid_string[UUID_STRING_LEN + 1]; + struct ctl_table fake_table =3D { + .data =3D uuid_string, + .maxlen =3D UUID_STRING_LEN + }; + + if (write) + return -EPERM; =20 uuid =3D table->data; if (!uuid) { @@ -1664,12 +1667,8 @@ static int proc_do_uuid(struct ctl_table *table, int= write, void *buffer, spin_unlock(&bootid_spinlock); } =20 - sprintf(buf, "%pU", uuid); - - fake_table.data =3D buf; - fake_table.maxlen =3D sizeof(buf); - - return proc_dostring(&fake_table, write, buffer, lenp, ppos); + snprintf(uuid_string, sizeof(uuid_string), "%pU", uuid); + return proc_dostring(&fake_table, 0, buffer, lenp, ppos); } =20 static struct ctl_table random_table[] =3D { @@ -1704,13 +1703,11 @@ static struct ctl_table random_table[] =3D { { .procname =3D "boot_id", .data =3D &sysctl_bootid, - .maxlen =3D 16, .mode =3D 0444, .proc_handler =3D proc_do_uuid, }, { .procname =3D "uuid", - .maxlen =3D 16, .mode =3D 0444, .proc_handler =3D proc_do_uuid, }, --=20 2.35.1