From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.3 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8DC6DC4742C for ; Wed, 11 Nov 2020 10:18:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 39BFF20679 for ; Wed, 11 Nov 2020 10:18:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727378AbgKKKSk (ORCPT ); Wed, 11 Nov 2020 05:18:40 -0500 Received: from szxga07-in.huawei.com ([45.249.212.35]:7881 "EHLO szxga07-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726557AbgKKKSj (ORCPT ); Wed, 11 Nov 2020 05:18:39 -0500 Received: from DGGEMS403-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga07-in.huawei.com (SkyGuard) with ESMTP id 4CWLKc4TR8z6xQ3; Wed, 11 Nov 2020 18:18:28 +0800 (CST) Received: from [10.65.58.147] (10.65.58.147) by DGGEMS403-HUB.china.huawei.com (10.3.19.203) with Microsoft SMTP Server id 14.3.487.0; Wed, 11 Nov 2020 18:18:28 +0800 Subject: Re: [RESEND PATCH] libfs: fix error cast of negative value in simple_attr_write() To: Andrew Morton References: <1605000324-7428-1-git-send-email-yangyicong@hisilicon.com> <20201110111842.1bc76e9def94279d4453ff67@linux-foundation.org> CC: , , , , , From: Yicong Yang Message-ID: <0b3954a4-1ac9-c454-a0ea-1fa1be5975b8@hisilicon.com> Date: Wed, 11 Nov 2020 18:18:31 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.7.1 MIME-Version: 1.0 In-Reply-To: <20201110111842.1bc76e9def94279d4453ff67@linux-foundation.org> Content-Type: text/plain; charset="windows-1252" Content-Transfer-Encoding: 7bit X-Originating-IP: [10.65.58.147] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi, Thanks for reviewing this. On 2020/11/11 3:18, Andrew Morton wrote: > On Tue, 10 Nov 2020 17:25:24 +0800 Yicong Yang wrote: > >> 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. >> >> ... >> >> --- a/fs/libfs.c >> +++ b/fs/libfs.c >> @@ -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 */ > kstrtoull() takes an `unsigned long long *', but `val' is a u64. > > I think this probably works OK on all architectures (ie, no 64-bit > architectures are using `unsigned long' for u64). But perhaps `val' > should have type `unsigned long long'? the attr->set() takes 'val' as u64, so maybe we can stay it unchanged here if it works well. Thanks, Yicong > . >