Received: by 2002:ac0:a591:0:0:0:0:0 with SMTP id m17-v6csp747031imm; Sat, 7 Jul 2018 07:53:10 -0700 (PDT) X-Google-Smtp-Source: AAOMgpe6EyAptoQb0daeeL0L7fDW5KfWF17raWr70jEU6Aa3s9WOyWPS2sJJE1ef68LjGBn2lEmN X-Received: by 2002:a62:4255:: with SMTP id p82-v6mr14633732pfa.227.1530975190075; Sat, 07 Jul 2018 07:53:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530975190; cv=none; d=google.com; s=arc-20160816; b=jo23YSrBU+XxY0iKfnZGClejqESM6bQs/D63sNtUZQrZ7h9lkT+8SNpt5u8PKrb/IW Ix/nxdYibVP92i9J4+iiVwudfjiAS8FOvtRvMfPt9Jvjp3i3e7in30hxF6GXt0WVGd/X SAlCfnqkzIpQn87olKbTnrd0FPZ2sT2YoVL47UzYH50O43J880mC3mzqqRZe77QS6hUa B4gAXV5mtyCAigqTrX8KSU0Zy7vwBv3jyuI7GDxi+tJZ9LmgFe3i/Q8wVIDLE2H1+HmV zB6SnzZhkhkIRWgUcQzzHXxea8xC5I40m4lDZxMkc8TwxAZB4fk44JHfGxeL4ehcLu3y CbJQ== 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=luPQpcWML45obeQaB0ki7nKlPoSgzJ+sMLGxwYTGiXg=; b=ryti9cPQxY/miZ4t0gVruTGgOgZ01psRgIZq8CRWZYBIU4uYKEIACNkC6o3NGcaL8l XuEYCMICoblfiNK+4TDO5ay/bZ/InjGvrmbF3+w+qRrhaxvra5gN47U1MNf3tfcafDpE AHp+C2lTV5yKxGqSYWSucR/l8R7ReuBdpKwXgUJDxTiP5WwL/qytEZGD5QAwtSGudvv7 0ev5LjoB/OPJwlJ0GC+25VSrFRUVwSke0SPlz9hfe3La5gwp5O0chXCIewoIExCO3xZC 6vqNGr8ZY7mKqK1PUyQuxqKdyJZpUnPvI0Wy1Z/Mbr0xc3MTOm9cgZfnrCChmPh1hwvc Logw== 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 ce9-v6si10994000plb.364.2018.07.07.07.52.55; Sat, 07 Jul 2018 07:53:10 -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 S1753732AbeGGOwR (ORCPT + 99 others); Sat, 7 Jul 2018 10:52:17 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:53604 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753586AbeGGOwR (ORCPT ); Sat, 7 Jul 2018 10:52:17 -0400 Received: from localhost (unknown [37.168.185.56]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id A2C64D94; Sat, 7 Jul 2018 14:52:15 +0000 (UTC) Date: Sat, 7 Jul 2018 16:52:11 +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: <20180707145211.GA20706@kroah.com> References: <20180707140140.GA4941@kroah.com> 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 02:34:05PM +0000, Bernd Edlinger wrote: > On 07/07/18 16:01, Greg Kroah-Hartman wrote: > > 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? > > > > I did only a code review, but the range checks look really dangerously > wrong. > > The string is only zero-terminated because it is allocated in > kernfs_iop_get_link with body = kzalloc(PAGE_SIZE, GFP_KERNEL); Which is why it is allocated that way :) > I would recommend to explicitly place a termination in the > buffer, and not rely on the way how the buffer is allocated. Why not? We explicitly wanted the buffer created this way, so that we do not have to work out where to put the termination. We do that all the time in the kernel. > > And why is the first memcpy replacement needed? gcc doesn't say > > anything about that, does it? > > > > No, that is more or less for efficiency reasons, since it is writing > the NUL which is always overwritten with something different in the > next step. If the loop is executed zero times, there result is not > explicitly zero-terminated either, so using strcpy is somehow misleading. > > Well, I would say personal taste, if the loop below constructs > the string with memcpy the loop above can do the same. > > If you prefer the strcpy in the first loop, I have no strong > preference here. I'd prefer to change the least amount of code as possible :) thanks, greg k-h