Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp284924imm; Tue, 5 Jun 2018 20:00:16 -0700 (PDT) X-Google-Smtp-Source: ADUXVKKAzW5lbogrOX3nTkG77nrGGA4ichxwxpr+g1DOc3gNpZBLq6XFQz+N6h89H/LeCDgxbdb6 X-Received: by 2002:a63:af13:: with SMTP id w19-v6mr1097440pge.47.1528254016545; Tue, 05 Jun 2018 20:00:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528254016; cv=none; d=google.com; s=arc-20160816; b=jL9sLxYtH00B1S1DcW5IfkX4W7MTbAt8U4595dYjqUW2Fwu8lZNB6tWWrOLe+91AmC WIvXLIUC30a6P+KaPzb+QerGIwys5CV03ZeMV4DtLoWMgWKnFjkT0HAX019m6S7wwz9f S1QViQ5br16/YisstrROQk7zKSWVysuiBEaTc1yl/XCdzuPafcu9qbw8wyMPGITyxrun lzPbU7q6Mx68hBLL1EchE9IiHa3X+sq7c19CEU2tI0ztelnrhSUMJMqiINBJqtmxduhm nlGr59lIaIIdtCi+Vq9u6V1m5nE2bz5iROPkoQSHN3nv6bhO4y4H2xqnwzAls8uBtJA5 Akew== 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:dkim-signature:arc-authentication-results; bh=oYUIR2X64r6HUtqbw2QvVQG9/CNtq0XANrniFK+rJ6Q=; b=dDpMczzIyu52E9qHbkFNcI2bdok4khyBmiOicKWnbBhXVrZk8JFUiivNCRnL3DIFBw O/G0itoWUP6O9hTNOkybVw44wusAm9OQJFSJRodq1DHU+Le19W1MhbDN/6v1ErOgVVYo Cukf6814GoAOM6cHen/E58Ws+0bCrlrEAcqT7lI/ezoveBpwa673zNm9Tv+LgQhFUlPa ZdNacYiQC84Cy38ZGF91im7Ueurc3seY6+6B2j5JpHCXU76fs3Vxto5HDf/xPkl42nl0 3WsoytcydQTOs0tgYFyZ2m116bmvj/4XRd5dS9artq7rljtWi5zKD7NXwLS+NVdrucSA oSUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2017-10-26 header.b=bDiu/dXG; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x33-v6si49864014plb.512.2018.06.05.20.00.00; Tue, 05 Jun 2018 20:00:16 -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=@oracle.com header.s=corp-2017-10-26 header.b=bDiu/dXG; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932084AbeFFC7e (ORCPT + 99 others); Tue, 5 Jun 2018 22:59:34 -0400 Received: from userp2130.oracle.com ([156.151.31.86]:42082 "EHLO userp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752536AbeFFC7c (ORCPT ); Tue, 5 Jun 2018 22:59:32 -0400 Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w562uluh040249; Wed, 6 Jun 2018 02:59:12 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=date : from : to : cc : subject : message-id : references : mime-version : content-type : in-reply-to; s=corp-2017-10-26; bh=oYUIR2X64r6HUtqbw2QvVQG9/CNtq0XANrniFK+rJ6Q=; b=bDiu/dXGq9Qp+3GJtSgh8VDWe28FivprzB7ZEwx62h5xAQEePneRNLAJU6rcOfZroqmk w/E1kh4m7hReUgW9gnVlKXGopoq3097ZNm+xQ7PdhZs1Gc0QdwKuBzlodZeAVpJ+nQJ6 rpQqe44c6qS60UjLEGzN7eTso5jQU4DZct7ZH/8IYaS3G+0rOz7A14aPuM0Vs7cvQyt0 TGenNfSXMPF8lC7bE69E7FVr+an3edvaChvNmU2g/4hucaXx7Ur7l/3PH3gaQVYM6tVN PMRMV+6ul+Z3iIjlYbqA0OOTleINAFO8ooILZKO0fud8GzrO/qBrpk8YSwecSlIJGwhj 8g== Received: from userv0022.oracle.com (userv0022.oracle.com [156.151.31.74]) by userp2130.oracle.com with ESMTP id 2jbvyparuh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 06 Jun 2018 02:59:12 +0000 Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by userv0022.oracle.com (8.14.4/8.14.4) with ESMTP id w562xBbm020585 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 6 Jun 2018 02:59:11 GMT Received: from abhmp0006.oracle.com (abhmp0006.oracle.com [141.146.116.12]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id w562xA3X025895; Wed, 6 Jun 2018 02:59:10 GMT Received: from localhost (/67.169.218.210) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 05 Jun 2018 19:59:10 -0700 Date: Tue, 5 Jun 2018 19:59:08 -0700 From: "Darrick J. Wong" To: Eric Sandeen Cc: Arnd Bergmann , linux-xfs@vger.kernel.org, Eric Sandeen , Martin Sebor , Brian Foster , Dave Chinner , Dan Williams , Ross Zwisler , linux-kernel@vger.kernel.org Subject: Re: [PATCH V2] xfs: fix string handling in get/set functions Message-ID: <20180606025908.GR9437@magnolia> References: <20180525151421.2317292-1-arnd@arndb.de> <7aad9fc3-ed65-a016-d477-5d830e31cb43@sandeen.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <7aad9fc3-ed65-a016-d477-5d830e31cb43@sandeen.net> User-Agent: Mutt/1.9.4 (2018-02-28) X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=8915 signatures=668702 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=2 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1805220000 definitions=main-1806060031 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jun 05, 2018 at 02:49:20PM -0500, Eric Sandeen wrote: > From: Arnd Bergmann > > [sandeen: fix subject, avoid copy-out of uninit data in getlabel] > > gcc-8 reports two warnings for the newly added getlabel/setlabel code: > > fs/xfs/xfs_ioctl.c: In function 'xfs_ioc_getlabel': > fs/xfs/xfs_ioctl.c:1822:38: error: argument to 'sizeof' in 'strncpy' call is the same expression as the source; did you mean to use the size of the destination? [-Werror=sizeof-pointer-memaccess] > strncpy(label, sbp->sb_fname, sizeof(sbp->sb_fname)); > ^ > In function 'strncpy', > inlined from 'xfs_ioc_setlabel' at /git/arm-soc/fs/xfs/xfs_ioctl.c:1863:2, > inlined from 'xfs_file_ioctl' at /git/arm-soc/fs/xfs/xfs_ioctl.c:1918:10: > include/linux/string.h:254:9: error: '__builtin_strncpy' output may be truncated copying 12 bytes from a string of length 12 [-Werror=stringop-truncation] > return __builtin_strncpy(p, q, size); > > In both cases, part of the problem is that one of the strncpy() > arguments is a fixed-length character array with zero-padding rather > than a zero-terminated string. In the first one case, we also get an > odd warning about sizeof-pointer-memaccess, which doesn't seem right > (the sizeof is for an array that happens to be the same as the second > strncpy argument). > > To work around the bogus warning, I use a plain 'XFSLABEL_MAX' for > the strncpy() length when copying the label in getlabel. For setlabel(), > using memcpy() with the correct length that is already known avoids > the second warning and is slightly simpler. > > In a related issue, it appears that we accidentally skip the trailing > \0 when copying a 12-character label back to user space in getlabel(). > Using the correct sizeof() argument here copies the extra character. > > Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85602 > Fixes: f7664b31975b ("xfs: implement online get/set fs label") > Cc: Eric Sandeen > Cc: Martin Sebor > Signed-off-by: Arnd Bergmann > Signed-off-by: Eric Sandeen Working around strncpy warnings with memcpy? I guess... Reviewed-by: Darrick J. Wong --D > --- > > diff --git a/fs/xfs/xfs_ioctl.c b/fs/xfs/xfs_ioctl.c > index 82f7c83c1dad..596e176c19a6 100644 > --- a/fs/xfs/xfs_ioctl.c > +++ b/fs/xfs/xfs_ioctl.c > @@ -1828,13 +1828,13 @@ xfs_ioc_getlabel( > /* Paranoia */ > BUILD_BUG_ON(sizeof(sbp->sb_fname) > FSLABEL_MAX); > > + /* 1 larger than sb_fname, so this ensures a trailing NUL char */ > + memset(label, 0, sizeof(label)); > spin_lock(&mp->m_sb_lock); > - strncpy(label, sbp->sb_fname, sizeof(sbp->sb_fname)); > + strncpy(label, sbp->sb_fname, XFSLABEL_MAX); > spin_unlock(&mp->m_sb_lock); > > - /* xfs on-disk label is 12 chars, be sure we send a null to user */ > - label[XFSLABEL_MAX] = '\0'; > - if (copy_to_user(user_label, label, sizeof(sbp->sb_fname))) > + if (copy_to_user(user_label, label, sizeof(label))) > return -EFAULT; > return 0; > } > @@ -1870,7 +1870,7 @@ xfs_ioc_setlabel( > > spin_lock(&mp->m_sb_lock); > memset(sbp->sb_fname, 0, sizeof(sbp->sb_fname)); > - strncpy(sbp->sb_fname, label, sizeof(sbp->sb_fname)); > + memcpy(sbp->sb_fname, label, len); > spin_unlock(&mp->m_sb_lock); > > /* > > -- > To unsubscribe from this list: send the line "unsubscribe linux-xfs" in > the body of a message to majordomo@vger.kernel.org > More majordomo info at http://vger.kernel.org/majordomo-info.html