From patchwork Mon Mar 4 20:58:54 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 10838595 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5625017E9 for ; Mon, 4 Mar 2019 20:59:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 35ABF2A9A2 for ; Mon, 4 Mar 2019 20:59:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 23EC72A9A9; Mon, 4 Mar 2019 20:59:06 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BE7752A9A2 for ; Mon, 4 Mar 2019 20:59:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726080AbfCDU7F (ORCPT ); Mon, 4 Mar 2019 15:59:05 -0500 Received: from userp2120.oracle.com ([156.151.31.85]:36998 "EHLO userp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726061AbfCDU7F (ORCPT ); Mon, 4 Mar 2019 15:59:05 -0500 Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x24KsYlq191947 for ; Mon, 4 Mar 2019 20:59:03 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=date : from : to : subject : message-id : references : mime-version : content-type : in-reply-to; s=corp-2018-07-02; bh=/6o+ySwNfW7Ln8jLIQFNo+MZBzFi5mxmCpLz2J2gx5E=; b=KjChOEdz9vO/S46Ybu8qLhOud9XqIlZE/2KborywutIrOMjfze4dwuGNE4f9+FT7PELD jGP+587yNQ6s5L9ia5fjH2rbh6z5kS4X2BvV+obeoXO/CdKJ6zOf+niwyfWj+oAyvzlz QcWLRDzPLs3qsSnwUPT5/M5LANwPz+OuPRSeJ4/DW5WqK2xoaHVGgZmOyKpfSzsUW0Fu 1d1rlqzvhXB9ADXD5uUOsR6ieP2VV1+1gJrMir9i3puKPSXXHTMT7dbWAa5gG9zmvC+Z ybvjONaKgrwNU3sMkRj1bZAflhNnBR/h2hJs0ecEqGCN8UOIcWi5n3okQmEQ+3GeMAdv IQ== Received: from userv0022.oracle.com (userv0022.oracle.com [156.151.31.74]) by userp2120.oracle.com with ESMTP id 2qyjfr94ha-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Mon, 04 Mar 2019 20:59:03 +0000 Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by userv0022.oracle.com (8.14.4/8.14.4) with ESMTP id x24KwvaI018386 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Mon, 4 Mar 2019 20:58:58 GMT Received: from abhmp0011.oracle.com (abhmp0011.oracle.com [141.146.116.17]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id x24KwvHF024012 for ; Mon, 4 Mar 2019 20:58:57 GMT Received: from localhost (/10.159.248.244) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 04 Mar 2019 12:58:57 -0800 Date: Mon, 4 Mar 2019 12:58:54 -0800 From: "Darrick J. Wong" To: linux-xfs@vger.kernel.org Subject: [PATCH 24/23] xfs_io: don't walk off the end of argv in fzero_f Message-ID: <20190304205854.GA6520@magnolia> References: <155148280859.16677.6057998944865066232.stgit@magnolia> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <155148280859.16677.6057998944865066232.stgit@magnolia> User-Agent: Mutt/1.9.4 (2018-02-28) X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=9185 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1903040148 Sender: linux-xfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Darrick J. Wong The fzero_f function doesn't check that there are enough non-switch parameters to supply offset and length arguments to fallocate. As a result, we can walk off the end of the argv array and crash. A secondary problem is that we don't use getopt to detect the -k, which is not how most xfs_io commands work. Therefore, use getopt to detect the -k argument and rewire the offset and length interpretation code to check optind and use argv correctly. This bug is trivially reproduced by "xfs_io -c 'fzero -k 0' /some/file". Signed-off-by: Darrick J. Wong --- io/prealloc.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/io/prealloc.c b/io/prealloc.c index 9a372bae..6d452354 100644 --- a/io/prealloc.c +++ b/io/prealloc.c @@ -285,18 +285,24 @@ fzero_f( { xfs_flock64_t segment; int mode = FALLOC_FL_ZERO_RANGE; - int index = 1; + int c; - if (strncmp(argv[index], "-k", 3) == 0) { - mode |= FALLOC_FL_KEEP_SIZE; - index++; + while ((c = getopt(argc, argv, "k")) != EOF) { + switch (c) { + case 'k': + mode |= FALLOC_FL_KEEP_SIZE; + break; + default: + command_usage(&fzero_cmd); + } } + if (optind != argc - 2) + return command_usage(&fzero_cmd); - if (!offset_length(argv[index], argv[index + 1], &segment)) + if (!offset_length(argv[optind], argv[optind + 1], &segment)) return 0; - if (fallocate(file->fd, mode, - segment.l_start, segment.l_len)) { + if (fallocate(file->fd, mode, segment.l_start, segment.l_len)) { perror("fallocate"); return 0; }