Received: by 2002:ab2:2441:0:b0:1f3:1f8c:d0c6 with SMTP id k1csp37532lqe; Wed, 3 Apr 2024 21:09:53 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVsWzN2hS0J8DPnXsvRGzVD8hh5nDx9f1mBMzV+CSDQqkykR0KdBV7zELNHsiFdND0YP2M4GFQxdwzRnfT8SjgAZhnACIL2+jJwcmd1ug== X-Google-Smtp-Source: AGHT+IGxspleb5aW3MrQBG7QGVbPGVvkulR9grMj4UrBVOmTy7iKw000MtfhggnXolWXe1t011tt X-Received: by 2002:a05:6358:6583:b0:181:6575:1a23 with SMTP id x3-20020a056358658300b0018165751a23mr1199447rwh.6.1712203793216; Wed, 03 Apr 2024 21:09:53 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712203793; cv=pass; d=google.com; s=arc-20160816; b=m4Hb4Dq4mJQLrCgoUK6X4WpbI6zml9tAUrpvOBNno6vRLwlWqKTi1u3lSU3OAEhTkq 1VNmOx33Malb7uNBUTnJHATc8/ORqsTl5a+RyOgGsTpcEZm+x8Rww3PlByfvttYvg1dK 4gz8nVXjAJMUjudr7Jso9dkjflwg6nMfV/amam91HrwdlbbpzOOmsAJuc14HS0BuZHo6 +8a8R1HaQUF5amQ9GapAVzwDqqMzb7KgB8rMK9VPkzRrDzr7b9Yfy9S8yO745GycCEUw THQRx9gnp9S5z1+MnWt1kwpWqSKz/qY6pmNmQxLWJ49f+ci5ZO2AzAhIymvrzPDW6hM7 Tf9Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=2Tnf/JqmvOIZtBLVJwY7VQErV1KE6NDkwVDWid6kh50=; fh=4l7KSDlNppUhu4JOH1lAmTfi/U1EPKvKu2vG6fKhnA8=; b=PExcGUdNoRKDC7gx4SJ3gjgmlBo0F35CvE5Vf1F7YnfKfAC6c22mlRUNtfovpyNqNP 2K+Zj2nEbslbVeuli5Y/2+XvbH6ikfddwiG4O3aWiDFlobwMqmdEn4S8vuncFOKE1WVK JplVx7FJh/5uEtI9kDtThRkmYUmexnPaXiZdSjg78tx4wnh0vPN7OmfyfiWo9UKInx1O EMeqKh3rdw0tBg/ePWAx8N1A8vOQfLyWNXHgLkun+1Clhyz7Rn5N+kcV6inQKp3LEb4E z9OFYG9N8rlirYun103qV9zx22E7psvdFWlbWq0j1xSeXuCRp9uRNlhUT8+i6wPTWQBP G2sg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=hXspNnRb; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-130863-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-130863-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id j63-20020a638b42000000b005f057f31f96si14329034pge.60.2024.04.03.21.09.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 21:09:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-130863-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=@redhat.com header.s=mimecast20190719 header.b=hXspNnRb; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-130863-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-130863-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com 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 7CABBB2432E for ; Thu, 4 Apr 2024 04:08:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6D2BC210E4; Thu, 4 Apr 2024 04:08:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="hXspNnRb" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 A79B62F37 for ; Thu, 4 Apr 2024 04:07:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712203680; cv=none; b=dctZfIAaDliW0eM/BZf7UFbU5gGTWj71yLoJisV+ADyZ8hjg9rjiIogtfQ+0dD0zgHJ0LCH1atuolALaSzMewbhrTOs71vmzP0VFilMazIW3c6gU6DvV/GJ6HmsxFw8o7SQs4iiQrK+8PH5F0f8skPXxW6EJcP+5E0pWxAAOOgQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712203680; c=relaxed/simple; bh=cG0Z/Q8Vd/6CIRdn/7mt96uH+81CK7mUItk43ieg5Pg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=LxVTBc8aPZ34STl7b77i98qH4JONA4gpc4+DaJo2nETzJF7v0+cx90QXWPSXRDjwz3ACkf3MM8+LttTGoJwLw0gSB0QXjjiy8zKT/ayX3mVc6lsk2+80wz4pK/3XBqMsNQ8a33R/x5vjjMQdavnYccxkVlCdsHFf9vezZB+KX0Y= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=hXspNnRb; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712203677; 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: in-reply-to:in-reply-to:references:references; bh=2Tnf/JqmvOIZtBLVJwY7VQErV1KE6NDkwVDWid6kh50=; b=hXspNnRbWz6Z1V/PKVrg90oxgdAKvR8JRCMXBXAltbREe8h/zH2b11X/9ELm5xc9JkI+TU pl7B09FsKzS/NtaWn26D7i8+yzp2FWaZ3yl7ulRG3LsPv0x6UFi/Mns5x85MucI2pLYEIC E5Bo+c5O+K+55anb3/StOtg4wyL+S3E= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-604-6qohKH8hNYix2r2mMKHUzQ-1; Thu, 04 Apr 2024 00:07:55 -0400 X-MC-Unique: 6qohKH8hNYix2r2mMKHUzQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D9445185A782; Thu, 4 Apr 2024 04:07:54 +0000 (UTC) Received: from localhost (unknown [10.72.116.50]) by smtp.corp.redhat.com (Postfix) with ESMTPS id F31448173; Thu, 4 Apr 2024 04:07:53 +0000 (UTC) Date: Thu, 4 Apr 2024 12:07:46 +0800 From: Baoquan He To: Justin Stitt Cc: akpm@linux-foundation.org, Vivek Goyal , Dave Young , kexec@lists.infradead.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH v2] vmcore: replace strncpy with strscpy_pad Message-ID: References: <20240401-strncpy-fs-proc-vmcore-c-v2-1-dd0a73f42635@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240401-strncpy-fs-proc-vmcore-c-v2-1-dd0a73f42635@google.com> X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 On 04/01/24 at 06:39pm, Justin Stitt wrote: > strncpy() is in the process of being replaced as it is deprecated [1]. > We should move towards safer and less ambiguous string interfaces. > > Looking at vmcoredd_header's definition: > | struct vmcoredd_header { > | __u32 n_namesz; /* Name size */ > | __u32 n_descsz; /* Content size */ > | __u32 n_type; /* NT_VMCOREDD */ > | __u8 name[8]; /* LINUX\0\0\0 */ > | __u8 dump_name[VMCOREDD_MAX_NAME_BYTES]; /* Device dump's name */ > | }; > ... we see that @name wants to be NUL-padded. > > We're copying data->dump_name which is defined as: > | char dump_name[VMCOREDD_MAX_NAME_BYTES]; /* Unique name of the dump */ > ... which shares the same size as vdd_hdr->dump_name. Let's make sure we > NUL-pad this as well. > > Use strscpy_pad() which NUL-terminates and NUL-pads its destination > buffers. Specifically, use the new 2-argument version of strscpy_pad > introduced in Commit e6584c3964f2f ("string: Allow 2-argument > strscpy()"). > > Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1] > Link: https://github.com/KSPP/linux/issues/90 > Cc: linux-hardening@vger.kernel.org > Signed-off-by: Justin Stitt > --- > Changes in v2: > - don't mark buffers as __nonstring, instead use a string API (thanks Kees) > - Link to v1: https://lore.kernel.org/r/20240327-strncpy-fs-proc-vmcore-c-v1-1-e025ed08b1b0@google.com > --- > Note: build-tested only. > > Found with: $ rg "strncpy\(" > --- > fs/proc/vmcore.c | 5 ++--- > 1 file changed, 2 insertions(+), 3 deletions(-) > > diff --git a/fs/proc/vmcore.c b/fs/proc/vmcore.c > index 1fb213f379a5..5d08d4d159d3 100644 > --- a/fs/proc/vmcore.c > +++ b/fs/proc/vmcore.c > @@ -1370,9 +1370,8 @@ static void vmcoredd_write_header(void *buf, struct vmcoredd_data *data, > vdd_hdr->n_descsz = size + sizeof(vdd_hdr->dump_name); > vdd_hdr->n_type = NT_VMCOREDD; > > - strncpy((char *)vdd_hdr->name, VMCOREDD_NOTE_NAME, > - sizeof(vdd_hdr->name)); > - memcpy(vdd_hdr->dump_name, data->dump_name, sizeof(vdd_hdr->dump_name)); > + strscpy_pad(vdd_hdr->name, VMCOREDD_NOTE_NAME); > + strscpy_pad(vdd_hdr->dump_name, data->dump_name); LGTM, thx Acked-by: Baoquan He