Received: by 2002:ac0:a591:0:0:0:0:0 with SMTP id m17-v6csp707798imm; Sat, 7 Jul 2018 07:02:40 -0700 (PDT) X-Google-Smtp-Source: AAOMgpc9asmjGaV5H7nLyNgGyqhVxQHyXYN2MZfGKt+Ko8x1OQUCouNEmzvS8m/kWlu210IzauRl X-Received: by 2002:a63:5fc1:: with SMTP id t184-v6mr12333927pgb.183.1530972159981; Sat, 07 Jul 2018 07:02:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530972159; cv=none; d=google.com; s=arc-20160816; b=VxfX1EV4KvuDSj6aAKCtfJaoxoW+bxx35A8sO3i328lH3dQwTw5dYchDoNpfMP4YiN pcOg6/AwYznbo+58vn8RD7Dvv1628362FKpXJ72pLVDhvbByl6Oct0a77c17dOLJBUAV HSrJz1C0++Ras2KLWslmmLcb5e2lyRzwYVVsg6tjPgEBQHrfMFDiRP+uW6n74zIBrTg7 TR+1/qPXfC5Rb9zRyGDQnAvTlP5zllAUylHpyycWIO6josmTuUVqTungFnGM648egu+/ 3TX/awGxrUTBOkftgUYy3HWhDaMFjwDZMSo8FHwxJYFnDLDNc9mEKSPt4ScptZwNKYwK a0og== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:arc-authentication-results; bh=ozRbMmE4FEpH12xYSF7MmTA3XlJTMmYlPhoeek9zew0=; b=nQcoOt2E5vJVRHpUfclYCtyzK7OtIc7Jn6gFMBA3BBgEwYng0SzwrVAoBS5IUP6eQJ NZctDU8JtojWVxTHXq3gq8i4CXx4+hnHb8lpES5Wyid/qbwbj40zWTuBaBM8bKts49un Fe7Gi8WjrEGVqbFFkMUJbZjyLNYGtrSnMnMX0e7IS02HwXdb0yvlNRtjESe2iz5sXAJ/ VxFDTW+l5fJN1c/gNHfnh60qq4Lt8dXPIumB+AI24ERA5fIPAQohgYjSCZIfIXaS6pSX A/IG9BMl2eACeCdWvak5uOXyTGz47m8IdCAePJxfQIkMLSmOrDBbmzj+ST0AfmMmxZnZ B9OQ== ARC-Authentication-Results: i=1; mx.google.com; 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 x185-v6si10579105pfx.16.2018.07.07.07.02.25; Sat, 07 Jul 2018 07:02:39 -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; 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 S1753783AbeGGOBq (ORCPT + 99 others); Sat, 7 Jul 2018 10:01:46 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:52174 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753173AbeGGOBp (ORCPT ); Sat, 7 Jul 2018 10:01:45 -0400 Received: from localhost (unknown [37.168.185.56]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 50378D8E; Sat, 7 Jul 2018 14:01:44 +0000 (UTC) Date: Sat, 7 Jul 2018 16:01:40 +0200 From: Greg Kroah-Hartman To: Bernd Edlinger Cc: Tejun Heo , "linux-kernel@vger.kernel.org" Subject: Re: [PATCHv2] Fix range checks in kernfs_get_target_path Message-ID: <20180707140140.GA4941@kroah.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.0 (2018-05-17) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Jul 07, 2018 at 09:41:03AM +0000, Bernd Edlinger wrote: > The strncpy causes a warning [-Wstringop-truncation] here, > which indicates that it never appends a NUL byte to the path. > The NUL byte is only there because the buffer is allocated > with kzalloc(PAGE_SIZE, GFP_KERNEL), but since the range-check > is also off-by-one, and PAGE_SIZE==PATH_MAX the returned string > will not be zero-terminated if it is exactly PATH_MAX characters. > Furthermore also the initial loop may theoretically exceed PATH_MAX > and cause a fault. > > Signed-off-by: Bernd Edlinger > --- > fs/kernfs/symlink.c | 10 +++++++--- > 1 file changed, 7 insertions(+), 3 deletions(-) > > diff --git a/fs/kernfs/symlink.c b/fs/kernfs/symlink.c > index 08ccabd..c8b7d44a 100644 > --- a/fs/kernfs/symlink.c > +++ b/fs/kernfs/symlink.c > @@ -63,7 +63,10 @@ static int kernfs_get_target_path(struct kernfs_node > if (base == kn) > break; > > - strcpy(s, "../"); > + if ((s - path) + 3 >= PATH_MAX) > + return -ENAMETOOLONG; > + > + memcpy(s, "../", 3); > s += 3; > base = base->parent; > } > @@ -79,16 +82,17 @@ static int kernfs_get_target_path(struct kernfs_node > if (len < 2) > return -EINVAL; > len--; > - if ((s - path) + len > PATH_MAX) > + if ((s - path) + len >= PATH_MAX) > return -ENAMETOOLONG; > > /* reverse fillup of target string from target to base */ > kn = target; > + s[len] = '\0'; > while (kn->parent && kn != base) { > int slen = strlen(kn->name); > > len -= slen; > - strncpy(s + len, kn->name, slen); > + memcpy(s + len, kn->name, slen); > if (len) > s[--len] = '/'; > This last memcpy replacement has already been applied to my tree, from a patch from soeone else, so are you sure all of the other changes are also really needed? Why the extra \0 termination of a string that is already terminated? And why is the first memcpy replacement needed? gcc doesn't say anything about that, does it? thanks, greg k-h