Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp4246950pxb; Tue, 10 Nov 2020 11:23:28 -0800 (PST) X-Google-Smtp-Source: ABdhPJwMpnG3xIvJ/P1sTkYGyEQBVB1QrJtK4W54RMuQQlrVZ6v7CYBB1J39FGFyEEYVZoGKj3Z8 X-Received: by 2002:a05:6402:1214:: with SMTP id c20mr22094083edw.182.1605036208313; Tue, 10 Nov 2020 11:23:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605036208; cv=none; d=google.com; s=arc-20160816; b=bkmHoi/253oD6aW+Ap+idX7U2ecIGbYF0FTIbtj2c1qc9DS9QEJwq1tlrNQMTnqt3G exz5J8r0eAyKtAOMQ8CxITZatXD/+ctty9xQBV/Xsh+wfSktoDjrdAte3Jhp1RHL0xIs DMT1qd9+1XrZGwc3G0B5yhGcSR4DVJfcoPnTkNGoZAPNMYB3aKlV6PJ41R0hKphJ4yrH /BOL4uG/YRLHi0WXh4q2icAywwiOhQdNBgeD0dFYe5AyclESU8tI42Pp8P0ncN241uFL JSO5KHGLobjHzEBDnvwcNdGTA9DQnVUmo1H/MG/JW6c1OgbtLCG4+nWf0t1flW2NToy0 T/7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=iwXuvLcd0t61Ng20//EGmUHJuFelpx7YOKORernkYKI=; b=x9TVg7pL7PW01bYu/jklYIxSO7JC1P3p8iag2oh/GkbNcoC5hRADy8jG+HBJDt9O+F r5nCzGWRr6SCOxrJxM0COw4YbJ2nZL0SMMo08CHnVBbDNTfulzbmyyPwsMVOSyE6pfgu I72H8notVQ0W1o1PNODbGvQxcAYTF8N1Q9OaVYrPEDADDRv7OfC6ehrruVnUGrLRq43c nX+YpBlucWQW1oTSS2vNt1YiwTKUtdxCKal5Mu/wAhpgGXDOW8JySAeepRLEVNsp1xEY 4okftFWUuxDdh0TDM15P7QOP6MXQnAR7jlIzrmagfb+fJ+/XYfxE6q306vr7TP+5in+d 6Xpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=SXutxCxm; 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 ay12si9490095ejb.431.2020.11.10.11.23.04; Tue, 10 Nov 2020 11:23:28 -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; dkim=pass header.i=@kernel.org header.s=default header.b=SXutxCxm; 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 S1731499AbgKJTSo (ORCPT + 99 others); Tue, 10 Nov 2020 14:18:44 -0500 Received: from mail.kernel.org ([198.145.29.99]:56916 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726307AbgKJTSn (ORCPT ); Tue, 10 Nov 2020 14:18:43 -0500 Received: from localhost.localdomain (c-73-231-172-41.hsd1.ca.comcast.net [73.231.172.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id D3D512054F; Tue, 10 Nov 2020 19:18:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1605035923; bh=QyaQj8nTAcAePgBxv8GT7EUTzLqnjFIqlPDgD7W/gko=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=SXutxCxmOb4O7uW0nfSaG1esxskxJHgmpeYeSOOdlK9bujKeiUkijwgWvglSsfH0h 0UwJokCJsFTeHzDyS9tjtVxZEGXnYqzVLL2tQiOYsSse7EbDhsIVYigP4j4vK0GN2u 7zR074kr/3rwJbZkU/7M7V3lLz8Fw8aV1K2BKEj4= Date: Tue, 10 Nov 2020 11:18:42 -0800 From: Andrew Morton To: Yicong Yang Cc: , , , , , Subject: Re: [RESEND PATCH] libfs: fix error cast of negative value in simple_attr_write() Message-Id: <20201110111842.1bc76e9def94279d4453ff67@linux-foundation.org> In-Reply-To: <1605000324-7428-1-git-send-email-yangyicong@hisilicon.com> References: <1605000324-7428-1-git-send-email-yangyicong@hisilicon.com> X-Mailer: Sylpheed 3.5.1 (GTK+ 2.24.31; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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'?