From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753725AbbCPMSr (ORCPT ); Mon, 16 Mar 2015 08:18:47 -0400 Received: from a.ns.miles-group.at ([95.130.255.143]:65275 "EHLO radon.swed.at" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750862AbbCPMSp (ORCPT ); Mon, 16 Mar 2015 08:18:45 -0400 Message-ID: <5506CA23.8090106@nod.at> Date: Mon, 16 Mar 2015 13:18:43 +0100 From: Richard Weinberger User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: Geert Uytterhoeven CC: uml-devel , "linux-kernel@vger.kernel.org" Subject: Re: [uml-devel] [PATCH 07/15] hostfs: Remove open coded strcpy() References: <1426506079-26183-1-git-send-email-richard@nod.at> <1426506079-26183-8-git-send-email-richard@nod.at> In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Am 16.03.2015 um 13:03 schrieb Geert Uytterhoeven: > On Mon, Mar 16, 2015 at 12:41 PM, Richard Weinberger wrote: >> --- a/fs/hostfs/hostfs_kern.c >> +++ b/fs/hostfs/hostfs_kern.c >> @@ -105,11 +105,10 @@ static char *__dentry_name(struct dentry *dentry, char *name) > > This code looks fishy to me... > > First we have: > > len = strlen(root); > strlcpy(name, root, PATH_MAX); > > (I notice the code used strncpy() before. One difference with strlcpy() > is that strncpy() fills the remaining of the destination buffer with zeroes.) > > Then: > >> __putname(name); >> return NULL; >> } >> - if (p > name + len) { >> - char *s = name + len; > > Unless strlcpy() truncated the string (which is unlikely, as root > cannot be longer > than PATH_MAX?), s = name + len now points to the zero terminator. > So the below would copy just one single byte: > >> - while ((*s++ = *p++) != '\0') >> - ; >> - } >> + >> + if (p > name + len) >> + strcpy(name + len, p); >> + > > What is this code really supposed to do? Hostfs' __dentry_name() builds the real path. i.e, the prefix on the host side plus the requested path in UML. "strlcpy(name, root, PATH_MAX);" copies the host prefix into name and then the "strcpy(name + len, p);" copies the requested path into it. The trick is that both share the same buffer, allocated by dentry_path_raw(). Therefore this bounds check works: if (len > p - name) { __putname(name); return NULL; } Is it now clearer or did I miss something? I agree that this code is tricky. :) Thanks, //richard