Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp458837lqh; Thu, 28 Mar 2024 07:08:58 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXEOJCGvklVElxQew1UG2hOS+V9ptFu5BWvPr79svOUUEUT3niUgFl0vHgSBHlvcCQiB9UkxqWQB930PC6lyKVFPHBZfFQ2gkel9KoE5g== X-Google-Smtp-Source: AGHT+IFMTse1kHUMYd25rAKvzLylgzEtH1FhjViesmk2uhCGX1YpSF2CPZkXXJf7sP44Y1OOzAk1 X-Received: by 2002:a05:6a20:5494:b0:1a3:a821:f297 with SMTP id i20-20020a056a20549400b001a3a821f297mr3450050pzk.2.1711634938670; Thu, 28 Mar 2024 07:08:58 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711634938; cv=pass; d=google.com; s=arc-20160816; b=N6mfcktQaDLlwz4jwnRO152I8YGvxE8mwMFumYfY0mtdMTyVy+kto+PXGNKZZxmvv1 nUrxZDNS69p69ZXKCpp+Kf65kg8ErZ0l489nfKE2vJUmQJnyJJixQCcTZpsQuGgFgk47 MHJWWppfSvrexu8Kz1ycaVpKm7B01DZeX189MTDHrNuU1tln/o7pUtXsx2YB1V/mKt7F eImvMOhwS0V081lQ6onCblp3otHRxLq98rZG9FlfbLSKugnB2YYo1g1ox143WBX77apu gfy07NJdYqZVrWHVNK7X5aEiE8BC15K+VDYWMcasieNrSbIaiRSCRsnIY3QrQkJvXFu8 KNng== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=d095wkAl1j14IAFVHT/e19h8dAoWUNV1QlLln0/BFus=; fh=shiu1ze5LlV3jDK6Wbe7J25BCwY0L0CpbEWscI1tSdQ=; b=zD9+vgPAJaKy4ECwVa9scdXzDTe5zkOiEOgJVAg9qJj0rEMo190pFWWB+i6dtHtKVp SSAoXA48e+QBtAI3D1IGwc1Q/Mvae736oYQKhZrzR9m8UssgVcap6YMdiqSXo+3Ok02q OmrI7Ad9T5Bzg13wUnPbm61O3/Qm3N1MDVPwrQBa8P4mj9G+5qq+V4iO2dLvQrmdlknk 3CyNFBGGM/03glEuk4aYxYd/S8mwqKOYL9yQxbubhx4lFzn7G3x54LVBFCZaApA1Uz95 xTI8ioCi5VyBT/kl6guCMrqdTkX4sVZW6RkiBhxrGqZZcKbulExJLLGoZhsUZY/UJ5u6 jrVw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=R998RHJ3; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-123022-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-123022-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id m5-20020a632605000000b005e468bd4cadsi1555098pgm.787.2024.03.28.07.08.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 07:08:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-123022-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=R998RHJ3; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-123022-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-123022-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 64918B23AB4 for ; Thu, 28 Mar 2024 14:07:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9A55E1272C7; Thu, 28 Mar 2024 14:06:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="R998RHJ3" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AC9DC84D31; Thu, 28 Mar 2024 14:06:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711634808; cv=none; b=qRoMbQ0uxkfl+MOL3rvHFVyw/MqQ6XV3Snl/est9smpEbNC3gFl91boUJ/OZKET8I5UY3ZtW0RwaM/SVTPzNr0TWQJEUc8tS8WhrqSN9pOG7kztjyQrHyVLmgGbNy2pM5SsqWgjAEr8CplOaRassyRSr2UwBZyzrgsc1gdjN1P4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711634808; c=relaxed/simple; bh=yHFhPfrMMHs59SCQp+6DZsAO2LqRForPxbzKn/h9OZA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lnP6Gu+2h+Z0gfI1FOG37wqu+3ARzH10LbuN/gGkWignOQ1F77dpHWE5+28eLVOK/h9ts7gWgcWRtGDgVGwX0fcyuiHiAFI2CcRcHaIfFxNmEGPUeGXL9pUhFqDY0Bb75VU9+Yh1+vQChLZVXFCZR/zgN8sliwYbmN6wKWgBpqc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=R998RHJ3; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id C68FAC433C7; Thu, 28 Mar 2024 14:06:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711634808; bh=yHFhPfrMMHs59SCQp+6DZsAO2LqRForPxbzKn/h9OZA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=R998RHJ3bF9lTIX5YWthw9VZaOAKxAC8PYU9VG6hxbfvkV/2ojvYP+QAo5KdLlrQ7 /wz84TbbA9274ily1eOENho1tB+8cFC9giRCQegDvg4tPyUwccnz0Bdw4+I8Moyt5s Rw0NhibOpG0YPZHN8OLMstOKFp8ZFMWrowVFFP0BQ5sTlgZ4NiVcAIGqdCXFuNq3ne uKY3f9mdvItaaz+H+E9vnv4jxK/06AW1LCwyelKi2hAuNcupkYrRwVqZD+9E7z3+B5 dAytiqSnnjaHoDp+hvabvCDcEm3Rry4aP06xRnDKpxAYTsukGYcIucItdTYFK0MG4s JHhAW59Pcv4eg== From: Arnd Bergmann To: linux-kernel@vger.kernel.org, "Richard Russon (FlatCap)" , Jens Axboe Cc: Arnd Bergmann , linux-ntfs-dev@lists.sourceforge.net, linux-block@vger.kernel.org Subject: [PATCH 07/11] block/partitions/ldm: convert strncpy() to strscpy() Date: Thu, 28 Mar 2024 15:04:51 +0100 Message-Id: <20240328140512.4148825-8-arnd@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240328140512.4148825-1-arnd@kernel.org> References: <20240328140512.4148825-1-arnd@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Arnd Bergmann The strncpy() here can cause a non-terminated string, which older gcc versions such as gcc-9 warn about: In function 'ldm_parse_tocblock', inlined from 'ldm_validate_tocblocks' at block/partitions/ldm.c:386:7, inlined from 'ldm_partition' at block/partitions/ldm.c:1457:7: block/partitions/ldm.c:134:2: error: 'strncpy' specified bound 16 equals destination size [-Werror=stringop-truncation] 134 | strncpy (toc->bitmap1_name, data + 0x24, sizeof (toc->bitmap1_name)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ block/partitions/ldm.c:145:2: error: 'strncpy' specified bound 16 equals destination size [-Werror=stringop-truncation] 145 | strncpy (toc->bitmap2_name, data + 0x46, sizeof (toc->bitmap2_name)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ New versions notice that the code is correct after all because of the following termination, but replacing the strncpy() with strscpy_pad() or strcpy() avoids the warning and simplifies the code at the same time. Use the padding version here to keep the existing behavior, in case the code relies on not including uninitialized data. Signed-off-by: Arnd Bergmann --- block/partitions/ldm.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/block/partitions/ldm.c b/block/partitions/ldm.c index 38e58960ae03..2bd42fedb907 100644 --- a/block/partitions/ldm.c +++ b/block/partitions/ldm.c @@ -131,8 +131,7 @@ static bool ldm_parse_tocblock (const u8 *data, struct tocblock *toc) ldm_crit ("Cannot find TOCBLOCK, database may be corrupt."); return false; } - strncpy (toc->bitmap1_name, data + 0x24, sizeof (toc->bitmap1_name)); - toc->bitmap1_name[sizeof (toc->bitmap1_name) - 1] = 0; + strscpy_pad(toc->bitmap1_name, data + 0x24, sizeof(toc->bitmap1_name)); toc->bitmap1_start = get_unaligned_be64(data + 0x2E); toc->bitmap1_size = get_unaligned_be64(data + 0x36); @@ -142,8 +141,7 @@ static bool ldm_parse_tocblock (const u8 *data, struct tocblock *toc) TOC_BITMAP1, toc->bitmap1_name); return false; } - strncpy (toc->bitmap2_name, data + 0x46, sizeof (toc->bitmap2_name)); - toc->bitmap2_name[sizeof (toc->bitmap2_name) - 1] = 0; + strscpy_pad(toc->bitmap2_name, data + 0x46, sizeof(toc->bitmap2_name)); toc->bitmap2_start = get_unaligned_be64(data + 0x50); toc->bitmap2_size = get_unaligned_be64(data + 0x58); if (strncmp (toc->bitmap2_name, TOC_BITMAP2, -- 2.39.2