Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp3310945pxx; Mon, 2 Nov 2020 05:49:20 -0800 (PST) X-Google-Smtp-Source: ABdhPJx8Qh3MLxaY3CJ9B1WCERUyAIkOEF+MydjXCXgaESClMOjuI0UhBO0PZYMT66MTOpar+qIy X-Received: by 2002:a17:906:318f:: with SMTP id 15mr14830434ejy.180.1604324959842; Mon, 02 Nov 2020 05:49:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604324959; cv=none; d=google.com; s=arc-20160816; b=GWtbdGVYeBYmVXwmU/IaSP4qSWcncMYtlvbv/vkNxO2mxbAJ1u4MWomI/6E3O9NbKI /bNshZ4sHVdHUhNYe34jv2ikiEuQcn80GsRyZ/TycIwGHYHBzMa624Xo1c0rC9mWxrw6 W3orRgoLXLo69sqhG26Zj2+hukVVFZD8GglQTrGHsdj9QMXVNcUil7zN+WyljW8e+sc8 f2WaIyEOQFmSYZjqGGw8k0nJPxdWjyjMpsGmRJDIA/BmBzaML29n3jxyGVzkaCqsoae3 lk2ckCChjOVKCbI8LwwqanYmTXmmQjbQLP5/8W3LW2g8nIbCIvF5t/Wo1S0y37i3duK9 C+YA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=DVnJguRhgSuHhwcYKzQRaLJKdcuRSoN8zs/m3lzrT8A=; b=zS7WwxrU2kTpBm6s2EQqZfYpDWsbHjba0f2+93nQU6M0bmwyqXkWkXBMp3xc1W/X5e tgu18jUV8ycuO5QprUQgEp3fHNea1MA7nnTxmddFyLN5sU/UyvS65msaWqitMShnZRlQ TWNPmuChDWdqgez3WpnO3zTQqSGEipeVqRdI0ojxpVeZM5wiBfFaESaJf0ciddYbp4kl ++HVBukJ+i1E1IzndPFjoN6g2VYlQit4yCPRcE9YsYVYEWfbmUCaF87FsZXpkFcmBia6 BvBpVpPFlbh032DsZfjzDns8q8Gu9+9JgGJKjPdhgweVw64nWZnfLSmYay2Yb7eAhWG6 aPgw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Wj+M71N1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p3si14086483edd.200.2020.11.02.05.48.56; Mon, 02 Nov 2020 05:49:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Wj+M71N1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725870AbgKBNrd (ORCPT + 99 others); Mon, 2 Nov 2020 08:47:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42180 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725616AbgKBNrd (ORCPT ); Mon, 2 Nov 2020 08:47:33 -0500 Received: from mail-wm1-x343.google.com (mail-wm1-x343.google.com [IPv6:2a00:1450:4864:20::343]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BF32DC0617A6 for ; Mon, 2 Nov 2020 05:47:32 -0800 (PST) Received: by mail-wm1-x343.google.com with SMTP id k18so9560421wmj.5 for ; Mon, 02 Nov 2020 05:47:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to; bh=DVnJguRhgSuHhwcYKzQRaLJKdcuRSoN8zs/m3lzrT8A=; b=Wj+M71N1RdTes6u5Dk/LSSCt4EGD0c0ahuPykaBmLtqj5wgCCwDKpQorczrBwUZuxW jxmSqJxuntk6eSsX63IzrPdaSzAGwEA6hNvc4fkb12l+Oqvle+NIL9fTwBZAt1uhP6Wi TaDnKUVszH5mVBfI6frj/C4seQwVvbFzL213QQRQ7e65Ct8ONqO3adpQ7wRZwT/AT7l1 zNHx7Cn2WP7eY3Te4BoVsfclhGCL8OqkucRFhVDVXNiOld4XY55FKA41KkrpCSFGTunm U50Fsf5VY3ndb11+hIedidMU8LPxc6rH9DiRhPl+dPfocciUMjYV8rCKb2axq0GKBq4/ s39g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=DVnJguRhgSuHhwcYKzQRaLJKdcuRSoN8zs/m3lzrT8A=; b=C238yCXzTKC1RTC3Dg7HE9W/7emYO6XmJ08sDfhyl4EowHxN815BNsag9YLswdRpDn xo1AczAJNeZYNrpiaQBZ7OfNk4Um8PuD6pyxJmoihAOGN1NealCXBaOMlvB4ETMYzJmY OfuGoQcq082m1AsUVI89U+5AZUjzrOONZTmrcr6nFQ7dduMomaWPj4l7K2pTJyyq7ySr Pa1+/LRj4hdFGvKtPicC064cHixbYsxGT5uBmBkNTqwTY5RT90X8z3hCXPczIYYz3huC ED4VDfBcL2kOm3RBh3AoCKo3Bjq3xHx4GKD+LtomWLU2sfjxDljNhuNzcoBeYSU/TzFV tTpw== X-Gm-Message-State: AOAM5308p4O57LDx3NAEgBXnm90D+lRgwpMQ9jlElUAXPjl2X1XyNpSs ++DewzJNRsxyKHVXl1Fif/M34LQjg1NmNA== X-Received: by 2002:a1c:bac1:: with SMTP id k184mr11862826wmf.76.1604324851469; Mon, 02 Nov 2020 05:47:31 -0800 (PST) Received: from dell ([91.110.221.242]) by smtp.gmail.com with ESMTPSA id s11sm22102143wrm.56.2020.11.02.05.47.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Nov 2020 05:47:30 -0800 (PST) Date: Mon, 2 Nov 2020 13:47:29 +0000 From: Lee Jones To: "gregkh@linuxfoundation.org" Cc: David Laight , "arnd@arndb.de" , "linux-kernel@vger.kernel.org" , Rodolfo Giometti , "Eurotech S.p.A" , Geert Uytterhoeven Subject: Re: [PATCH 1/2] misc: c2port: core: Make copying name from userspace more secure Message-ID: <20201102134729.GD4488@dell> References: <20201102111211.1047972-1-lee.jones@linaro.org> <20201102114903.GN4127@dell> <20201102121150.GA663356@kroah.com> <20201102124301.GC4488@dell> <20201102125910.GA1008111@kroah.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20201102125910.GA1008111@kroah.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 02 Nov 2020, gregkh@linuxfoundation.org wrote: > On Mon, Nov 02, 2020 at 12:43:01PM +0000, Lee Jones wrote: > > On Mon, 02 Nov 2020, gregkh@linuxfoundation.org wrote: > > > > > On Mon, Nov 02, 2020 at 11:49:03AM +0000, Lee Jones wrote: > > > > On Mon, 02 Nov 2020, David Laight wrote: > > > > > > > > > From: Lee Jones > > > > > > Sent: 02 November 2020 11:12 > > > > > > > > > > > > strncpy() may not provide a NUL terminator, which means that a 1-byte > > > > > > leak would be possible *if* this was ever copied to userspace. Ensure > > > > > > the buffer will always be NUL terminated by using the kernel's > > > > > > strscpy() which a) uses the destination (instead of the source) size > > > > > > as the bytes to copy and b) is *always* NUL terminated. > > > > > > > > > > > > Cc: Rodolfo Giometti > > > > > > Cc: "Eurotech S.p.A" > > > > > > Reported-by: Geert Uytterhoeven > > > > > > Acked-by: Arnd Bergmann > > > > > > Signed-off-by: Lee Jones > > > > > > --- > > > > > > drivers/misc/c2port/core.c | 2 +- > > > > > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > > > > > > > > > diff --git a/drivers/misc/c2port/core.c b/drivers/misc/c2port/core.c > > > > > > index 80d87e8a0bea9..b96444ec94c7e 100644 > > > > > > --- a/drivers/misc/c2port/core.c > > > > > > +++ b/drivers/misc/c2port/core.c > > > > > > @@ -923,7 +923,7 @@ struct c2port_device *c2port_device_register(char *name, > > > > > > } > > > > > > dev_set_drvdata(c2dev->dev, c2dev); > > > > > > > > > > > > - strncpy(c2dev->name, name, C2PORT_NAME_LEN - 1); > > > > > > + strscpy(c2dev->name, name, sizeof(c2dev->name)); > > > > > > > > > > strscpy() doesn't zero fill so if the memory isn't zeroed > > > > > and a 'blind' copy to user of the structure is done > > > > > then more data is leaked. > > > > > > > > > > strscpy() may be better, but rational isn't right. > > > > > > > > The original patch zeroed the data too, but I was asked to remove that > > > > part [0]. In your opinion, should it be reinstated? > > > > > > > > [0] https://lore.kernel.org/patchwork/patch/1272290/ > > > > > > Just keep the kzalloc() part of the patch, this portion makes no sense > > > to me. > > > > Can do. > > > > > But if you REALLY want to get it correct, call dev_set_name() > > > instead please, as that is what it is there for. > > > > The line above isn't setting the 'struct device' name. It looks as > > though 'struct c2port' has it's own member, also called 'name'. As to > > how they differ, I'm not currently aware. Nor do I wish to mess > > around with the semantics all that much. > > > > Going with suggestion #1. > > As the "device" already has a name, I suggest just getting rid of this > name field anyway, no need for duplicates. That definitely goes against the point I made above: "Nor do I wish to mess around with the semantics all that much." It looks as though the device name 'c2port%d' varies greatly to the requested name 'uc'. I don't have enough knowledge of how user- space expects to use the provided sysfs entries to be able to competently merge/decide which of these should be kept and which to discard. Hopefully one of the authors/maintainers are reading this and can come up with an acceptable solution. -- Lee Jones [李琼斯] Senior Technical Lead - Developer Services Linaro.org │ Open source software for Arm SoCs Follow Linaro: Facebook | Twitter | Blog