Received: by 2002:ac0:950c:0:0:0:0:0 with SMTP id f12csp3868677imc; Thu, 14 Mar 2019 07:08:11 -0700 (PDT) X-Google-Smtp-Source: APXvYqwcoxVhb1LMHHitOWjttvBZEtYiZLj+WUq9mwLB6jz3+Vybu2XzXJ1vVDWY2kxp0kiFIyCT X-Received: by 2002:a63:441b:: with SMTP id r27mr15307775pga.36.1552572491638; Thu, 14 Mar 2019 07:08:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552572491; cv=none; d=google.com; s=arc-20160816; b=p5mlvMAQvWVqe7nJoCriXu/xlHwnkrcj1FlNv2Mzc4iRAEYhGjSEDwMgMDR4ZA++eD nVL45rAv7MfUvnQTuEo2hL/b0gH67c4G9s9/7sACEbOAweRCzP8VRBtruT8K1qs5/ALd Dip9CQjiy2zQkDwfWgdOzYlfCdnnic5LF32qNrO8MpqKO4gXrlAovC7E7+qnqMcnAb9z /lNqc4KZMcc87xWAbQH9aQDItbdr/oTFkZxm8aVCaKtl0nOkdkxENjT6WZ2e0KXq9gTq uOXObchWldMtqjlMbaO9k79KDaXZQ9RH4nlvXrPieqLvgchvhpTCEG/s1cKQrk1+6Vsq iaJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=VRwFdE13s8Q6PG8Mo5e/XaK0pY3ZbFPh1fheC9WehV0=; b=Yvwd2q9PC6m1YFrQzz+cew8HevGjVIU9KRhEPnWDd+cJCu5V/6bVkKFty4i3+ix9Vv 4xXahLg20hxoGS/UAqV6fyy1waoMpT5b8MHnrbAWVUqNDiAfUB7YDOZyzzcBodD/Gb2e BjR7IsJOa2/uH7JM1oh86bm5JwQ5EZqNgV04gDfy2f1FdvQCyh6JHZv6uGhvnP2fmZGe cq1C1kJnqMwCHhigZPYtPVXKZpwVUTGFVguIbg5cZUskFdEecPDVGIj9xYRThjVKd7u9 v+SszesJH67halVC0xnO5KLBYLV+gMxZMTIk9XiOXcmSq/e5LAutioj33dFupTVhhWmT acfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=gtZgeMLN; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y4si13661965plk.30.2019.03.14.07.07.56; Thu, 14 Mar 2019 07:08:11 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=gtZgeMLN; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727610AbfCNOG3 (ORCPT + 99 others); Thu, 14 Mar 2019 10:06:29 -0400 Received: from mail-lf1-f65.google.com ([209.85.167.65]:34430 "EHLO mail-lf1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727164AbfCNOG1 (ORCPT ); Thu, 14 Mar 2019 10:06:27 -0400 Received: by mail-lf1-f65.google.com with SMTP id y18so4346985lfe.1 for ; Thu, 14 Mar 2019 07:06:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=VRwFdE13s8Q6PG8Mo5e/XaK0pY3ZbFPh1fheC9WehV0=; b=gtZgeMLNIFZyr2rsu5BZQATbu3VfApodl6aSheQzxpYOcOFmNSImOjJ2EFjXDeQZc2 ZCP6uH9FdlMjtl/CaDXYwlBtCeUD3Qdo+bZ3qjW7AhVr6yySiCi1ba0orINGSLmYPefA oi5KHFQHyH1/sxf8eTg2FfDGsihFBm5TDDvTQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=VRwFdE13s8Q6PG8Mo5e/XaK0pY3ZbFPh1fheC9WehV0=; b=bb/AayYjB/gO3JKT1/FEcm7eOWpjrok3odzT80aQPW8A2cc7UdF/GdPoyfBLcdGPBZ 446pTiNd1U6s+oge8n1IMkeYSMPgFKo5ycprHs+xK+0tAcN/VTQORVtoXe/uCwZ6Fdx7 n0BTZ7Uhpvd3AuISzocOR6n+E/vZzY8URspgW6X7rzNDgLTvVsXAYnnpmraGdQ8ewp3L MPYlw6aFrw8i/9sYn7jYFipHcpTF7R8tu/96NUagSMc+Uptfds11R4rMnjhf+R0UioR0 v2/UFQzExO+C1OMHty5uiDg/Njro+z+xeb7czREiB9LLhZiE6SpxQbFXou1q2grTX9hJ 19cw== X-Gm-Message-State: APjAAAV+o6wh+XDXa+4XzZdPaaDOVSEr35lsxV+VE5E0prWhLq4HD5AE DkdMYzNjo4viLjVETt5mIdG20Q== X-Received: by 2002:ac2:51a1:: with SMTP id f1mr5547536lfk.25.1552572384960; Thu, 14 Mar 2019 07:06:24 -0700 (PDT) Received: from prevas-ravi.prevas.se ([81.216.59.226]) by smtp.gmail.com with ESMTPSA id t14sm2733709lfe.51.2019.03.14.07.06.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 14 Mar 2019 07:06:24 -0700 (PDT) From: Rasmus Villemoes To: Pavel Machek , =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , Jacek Anaszewski Cc: LKML , Rasmus Villemoes , linux-leds@vger.kernel.org Subject: [PATCH v2 1/6] leds: netdev trigger: use memcpy in device_name_store Date: Thu, 14 Mar 2019 15:06:14 +0100 Message-Id: <20190314140619.3309-2-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190314140619.3309-1-linux@rasmusvillemoes.dk> References: <20190313202615.22883-1-linux@rasmusvillemoes.dk> <20190314140619.3309-1-linux@rasmusvillemoes.dk> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If userspace doesn't end the input with a newline (which can easily happen if the write happens from a C program that does write(fd, iface, strlen(iface))), we may end up including garbage from a previous, longer value in the device_name. For example # cat device_name # printf 'eth12' > device_name # cat device_name eth12 # printf 'eth3' > device_name # cat device_name eth32 I highly doubt anybody is relying on this behaviour, so switch to simply copying the bytes (we've already checked that size is < IFNAMSIZ) and unconditionally zero-terminate it; of course, we also still have to strip a trailing newline. This is also preparation for future patches. Signed-off-by: Rasmus Villemoes --- drivers/leds/trigger/ledtrig-netdev.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/leds/trigger/ledtrig-netdev.c b/drivers/leds/trigger/ledtrig-netdev.c index 3dd3ed46d473..8d476b92f58c 100644 --- a/drivers/leds/trigger/ledtrig-netdev.c +++ b/drivers/leds/trigger/ledtrig-netdev.c @@ -122,7 +122,8 @@ static ssize_t device_name_store(struct device *dev, trigger_data->net_dev = NULL; } - strncpy(trigger_data->device_name, buf, size); + memcpy(trigger_data->device_name, buf, size); + trigger_data->device_name[size] = 0; if (size > 0 && trigger_data->device_name[size - 1] == '\n') trigger_data->device_name[size - 1] = 0; -- 2.20.1