Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp150021lqh; Wed, 27 Mar 2024 18:48:46 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUKKO2fig5bKFc0cMFAdVy62kpVxTKL0pGNLNglHtDb4w5oFZicZWb3/OAIQXHovcSYdezh/fAq69JwKGhyEykJPaxoK1yKs867z8VQvw== X-Google-Smtp-Source: AGHT+IFBBgmMqOLTTf0crPVD2kK3peWRdMWCHbc1vdQuRT2PZqOBogqOu2o4oN+giti586vQDgQl X-Received: by 2002:a17:906:ad2:b0:a4e:f82:375e with SMTP id z18-20020a1709060ad200b00a4e0f82375emr711638ejf.73.1711590526825; Wed, 27 Mar 2024 18:48:46 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711590526; cv=pass; d=google.com; s=arc-20160816; b=OuCON6Ypgneqb09ANdHLwigji3muFkPpzf17B7HvbdNHylhw2F5O7KryZpewpxKdF1 V2i44DuwJzIFCDMJBTSTFR0T3cqgGNEjqgrcsKInnYfdOHMpQnpWk9LCiWWQj1T/Sjw1 rkUYFDBoqlxkJxLm5X1sgsrGf8l79Zp7gpEj8ogKJdK084+2dz/+1d9cZ1jKoWRCbN6U uNjItyXFGtTU8X1dtuzqCGWPC28xnkbTLCzCvZgqsH+FP8aLpHia7dm/MQ9/UeeR613E EyEWbWYh1IjSOFW8ej0/6vJcTZyd2KNsSbD2k0t8hYXJWz1m8Iri07Y7UmEPuSE24krt +7Lw== 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=ms3mo6grZ1J92R+XQiZMnioOtLmeHwZYPWNCbExS/N4=; fh=/kmWtZ609IC4mbVqKhNoZ3pumukeb//VdX4x2883A4k=; b=sytsntSQGrDGGlQKylpCw8MIrLbIo5uY/mwzgKcmp/wBLXwfbz3qVtNlTEQs+6maeZ eKYp4LxVnDJemIxwcegTkGr0jHdoNTmNOZ6fHEwDiU8RuwTIQjoLVEl2W24qfbJnBXSZ AwmKaaO7SVzmffYIloJLSe2wulw6p8XJPbpn87piJj4ciHokVbnKe4Y/8g6leif8IDvY UdijuXi4fSB7dPdeJcEm9aVkp3PP7/ZbNVpYXBMiPyk0Zx74/fYBZXLUdqR8o0VkRS2/ k60646yWL+kHEKjjFZ/wU5LsMF62+zh1GDJZSwRvn5dWRjP+HIZNyp5e1mcGqY2oLNds 4sAA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=CFJDa28K; 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-122303-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-122303-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id q20-20020a1709060f9400b00a474e73906bsi167397ejj.796.2024.03.27.18.48.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 18:48:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-122303-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=CFJDa28K; 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-122303-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-122303-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 8D99A1F2AB99 for ; Thu, 28 Mar 2024 01:48:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2F5FC2209B; Thu, 28 Mar 2024 01:48:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="CFJDa28K" 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 C585320332 for ; Thu, 28 Mar 2024 01:48:37 +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=1711590519; cv=none; b=WcsZH757uL/sR2Puk3LYuVMgBFFNfhburqLDoxiN1cCcSZYOOnLOJYOl/0iqDQAysnz7d6ArWa3jLj1s+TfXNSXkOzy3HXvKsBZXBSEMcen03JLrB3dxU/38lWTb2i4LtTRtn7Q7yjgvvUUQL98+RaGdg+zjfcBoCnTZjbq6KCc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711590519; c=relaxed/simple; bh=eOkbu3OBv4Si34quW3MgK7iQ7dO1Yuh23hV9JYBs7P4=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=EmO4xQCiU5oANWwQRoG4gwLxDa+q/CjnQr1FiYueFg0bOgQ6dlcUqctc3AMvKMzWyP7eZil4y+gkk4Lu68WVGCebGdwFecAw+kwI6hVBBnBnssaqm82Ke4tF/XRt+sA+TB6SisCTlHGQsRj+iwJQ+AhIkf6X7XbrZgn61Rrh4Os= 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=CFJDa28K; 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=1711590516; 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=ms3mo6grZ1J92R+XQiZMnioOtLmeHwZYPWNCbExS/N4=; b=CFJDa28KSV9dRMvT8sSdE4ECy31J5rrq9nzsaIkeGceXA2SBxRrtt9qzsW7/oDS4ZWBmSp ILRaRE1eSx0RXlVMDBY5XT/6PM9SmkyaDPVaiWWLae7UVCXCcKqE3qfRzWtuB2NRb7wQ9N HpkFqSSFMTJhLNGGZXwJ1M1uPk451OY= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-294-lFeLVmLoN8m1AheDhDE6vA-1; Wed, 27 Mar 2024 21:48:32 -0400 X-MC-Unique: lFeLVmLoN8m1AheDhDE6vA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (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 03AA02803622; Thu, 28 Mar 2024 01:48:32 +0000 (UTC) Received: from localhost (unknown [10.72.116.12]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 17290111E3F3; Thu, 28 Mar 2024 01:48:30 +0000 (UTC) Date: Thu, 28 Mar 2024 09:48:23 +0800 From: Baoquan He To: Justin Stitt Cc: 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] vmcore: replace strncpy with strtomem Message-ID: References: <20240327-strncpy-fs-proc-vmcore-c-v1-1-e025ed08b1b0@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: <20240327-strncpy-fs-proc-vmcore-c-v1-1-e025ed08b1b0@google.com> X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 On 03/27/24 at 09:10pm, Justin Stitt wrote: > strncpy() is in the process of being replaced as it is deprecated in > some situations [1]. While the specific use of strncpy that this patch > targets is not exactly deprecated, the real mission is to rid the kernel > of all its uses. > > 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 can see that both `name` and `dump_name` are u8s. It seems `name` > wants to be NUL-padded (based on the comment above), but for the sake of > symmetry lets NUL-pad both of these. > > Mark these buffers as __nonstring and use strtomem_pad. Thanks. I didn't build, wondering if '__nonstring' has to be set so that strtomem_pad() can be used. Thanks Baoquan > > 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 > --- > Note: build-tested only. > > Found with: $ rg "strncpy\(" > --- > fs/proc/vmcore.c | 5 ++--- > include/uapi/linux/vmcore.h | 4 ++-- > 2 files changed, 4 insertions(+), 5 deletions(-) > > diff --git a/fs/proc/vmcore.c b/fs/proc/vmcore.c > index 1fb213f379a5..5d7ecf3b75e8 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)); > + strtomem_pad(vdd_hdr->name, VMCOREDD_NOTE_NAME, 0); > + strtomem_pad(vdd_hdr->dump_name, data->dump_name, 0); > } > > /** > diff --git a/include/uapi/linux/vmcore.h b/include/uapi/linux/vmcore.h > index 3e9da91866ff..7053e2b62fa0 100644 > --- a/include/uapi/linux/vmcore.h > +++ b/include/uapi/linux/vmcore.h > @@ -11,8 +11,8 @@ 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 */ > + __u8 name[8] __nonstring; /* LINUX\0\0\0 */ > + __u8 dump_name[VMCOREDD_MAX_NAME_BYTES] __nonstring; /* Device dump's name */ > }; > > #endif /* _UAPI_VMCORE_H */ > > --- > base-commit: 928a87efa42302a23bb9554be081a28058495f22 > change-id: 20240327-strncpy-fs-proc-vmcore-c-b18d761feaef > > Best regards, > -- > Justin Stitt >