Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp1741111pxb; Sat, 14 Nov 2020 00:13:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJyU2zLEfNC/g2VmrHOSPr784XreFJQ5MNKfZzYacHeuF2WBhGfeLErsA2qn9a30pLHdRklm X-Received: by 2002:a17:906:2582:: with SMTP id m2mr5733853ejb.172.1605341618652; Sat, 14 Nov 2020 00:13:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605341618; cv=none; d=google.com; s=arc-20160816; b=dgl8436F2V/lMWgH5teRxOymNKx9YoMQ8v1HdBTsy9bEj54Zo/+aMrh3Msdy1FNeI8 NPF0dqLSbJrZbUEOlNTmIjbKrscM50mbcvHrmePKmWkOjBkIl2F/i+nHH2BtbxPOXQiv Re85Lis3LcM/HGH5UKWp2e0rVH6vCECVUQq1S2MnMkXapRrxOCIx648HW96X4oncC/G1 JQqDNnQ/YlDlMb3dNE3BAD/HvWydr1ZviQoJN8jQbXwsEGdNIEDo+6vJM1efMV3CcyJ7 vbwwfsLdAy9tMNni1Ba/sSNZB57Shgfxnv4SWVVylBMUZ3K/pV+QrL5TbxfIfnTu7F2V vDYg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:subject:cc:to:from; bh=v46sF+94Lrk+NgyxbfDxHdVis6bHM0g0dYXCH9dw3cE=; b=I5b88ARf8nefQvWdBa9UDIuBDi2E0NbKIYuE6bT4WHdi5vkItZfBDIKhcer50iAqBC 1W+A/iY6Mgq8OqB66BTzt804NRBvY6RmXjt5OSvHnVEWTBz00YTXTYUl2i+6qgHJqLhN 18VzmGtS3WihnGXLjILc5VyMny+etrEn9dW0qUkl8wKn/WruFyZyEHe29eY2lkeHM91n kPjbBT99SLV9UVC3UnxyHMZd6JhYvj4yTehXH//xJK2BcrC1NLRugWiUJhevNFEOfqtI HNz25QJIk3je4Bn0+M+lvpRYZkFNqqGxcMeo2/7RrT7kOtVbm0nt28g5fDIddhVr7HCn nZdA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u23si7957480ejj.163.2020.11.14.00.13.16; Sat, 14 Nov 2020 00:13:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726547AbgKNILM (ORCPT + 99 others); Sat, 14 Nov 2020 03:11:12 -0500 Received: from szxga07-in.huawei.com ([45.249.212.35]:7899 "EHLO szxga07-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726522AbgKNILM (ORCPT ); Sat, 14 Nov 2020 03:11:12 -0500 Received: from DGGEMS402-HUB.china.huawei.com (unknown [172.30.72.58]) by szxga07-in.huawei.com (SkyGuard) with ESMTP id 4CY7Lt2sNFz761H; Sat, 14 Nov 2020 16:10:46 +0800 (CST) Received: from localhost.localdomain (10.67.165.24) by DGGEMS402-HUB.china.huawei.com (10.3.19.202) with Microsoft SMTP Server id 14.3.487.0; Sat, 14 Nov 2020 16:10:48 +0800 From: Yicong Yang To: , , , CC: , , , , Subject: [PATCH v3] libfs: fix error cast of negative value in simple_attr_write() Date: Sat, 14 Nov 2020 16:09:16 +0800 Message-ID: <1605341356-11872-1-git-send-email-yangyicong@hisilicon.com> X-Mailer: git-send-email 2.8.1 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.67.165.24] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The attr->set() receive a value of u64, but simple_strtoll() is used for doing the conversion. It will lead to the error cast if user inputs a negative value. Use kstrtoull() instead of simple_strtoll() to convert a string got from the user to an unsigned value. The former will return '-EINVAL' if it gets a negetive value, but the latter can't handle the situation correctly. Make 'val' unsigned long long as what kstrtoull() takes, this will eliminate the compile warning on no 64-bit architectures. Fixes: f7b88631a897 ("fs/libfs.c: fix simple_attr_write() on 32bit machines") Signed-off-by: Yicong Yang --- Change since v1: - address the compile warning for non-64 bit platform. Change since v2: Link: https://lore.kernel.org/linux-fsdevel/1605000324-7428-1-git-send-email-yangyicong@hisilicon.com/ - make 'val' unsigned long long and mentioned in the commit Link: https://lore.kernel.org/linux-fsdevel/1605261369-551-1-git-send-email-yangyicong@hisilicon.com/ fs/libfs.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index fc34361..7124c2e 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -959,7 +959,7 @@ ssize_t simple_attr_write(struct file *file, const char __user *buf, size_t len, loff_t *ppos) { struct simple_attr *attr; - u64 val; + unsigned long long val; size_t size; ssize_t ret; @@ -977,7 +977,9 @@ ssize_t simple_attr_write(struct file *file, const char __user *buf, goto out; attr->set_buf[size] = '\0'; - val = simple_strtoll(attr->set_buf, NULL, 0); + ret = kstrtoull(attr->set_buf, 0, &val); + if (ret) + goto out; ret = attr->set(attr->data, val); if (ret == 0) ret = len; /* on success, claim we got the whole input */ -- 2.8.1