Received: by 2002:ab2:620c:0:b0:1ef:ffd0:ce49 with SMTP id o12csp801920lqt; Tue, 19 Mar 2024 04:33:16 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXnNAg/4IoKlSJudgpTeofYDKDEgG2Iv3sMV2qd4UPv2EW9KH63EVTb3314hbQfoclsLYFer2HTqDl0iaIT5d17ijtODDuYYbO1Asm96Q== X-Google-Smtp-Source: AGHT+IE96m+6bC1sJzAvxePQhwrrK2+dC/Qh/mLBwaU/MfLBdb79QsHHQN5MvH5YcyVUsH74fVSN X-Received: by 2002:a05:6402:3706:b0:56a:2b6b:42cd with SMTP id ek6-20020a056402370600b0056a2b6b42cdmr3761268edb.3.1710847996355; Tue, 19 Mar 2024 04:33:16 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710847996; cv=pass; d=google.com; s=arc-20160816; b=IcA3FrdESDXPeybgDtIEzkU75KwLqs56lbSgobF+z3Ym89dQCmthlCK1SNSSVGZXoS ZpUSxZbaS6zbIb6KQwUULo6CAm5O0BkZjc5Puig01ABtsrHMT/j261TFqZZ1Qcfjys0h FqVLboKTEpPezfSnFMDiyRRxW1OFwIO1GhSRAojLerBIUmMFVGJgdYfj1EhpWKCqOQXw QBHJjfNtypfjyZ7fBoHVNo/8Fk6qicxAD4EuD6BjndAkmm12KImNGfG1+TmagrdsIAJB eT4n5xcZE8XS2VEQVrhExQdzAZ45CKJfgJfExAuVPzmHeNmLEZECYoB1Uz44H8Tw4/Ki XkNg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=qfNLU6gH3h173DxGkkW6e1wPfkH0t72ZqK6qiyXupFE=; fh=4vN4thPQ5z49h0k3wc47Ze2ZGwmxH293ZHjdupTAu0I=; b=u9/IDxNzR0SglIHIjToLQ6cGLPY0alPsGGylDY+qVZwbsG5nM3V7+IB0kInXUnHt2E LkSjDUHlKUIXILQCzhu4lZFKNxbfLyIIHByXy7eEp1C7TD7PUmlNoIjN16Nbl6uGCOS6 bNJYWt+jpSdehdE0OsqZGyZvl6FnzjX8otYWH84ogm6Fp+dJira+y8PIjidUTNmYKGOQ ilyo5LAhzZV+pHZLuf8wshQgawfbU1M0JmsLhzgA5XN/Xg3ssEukzemDaBs2R3wEP/Up bpn7QG3DGgylD9PIbLYcF62ykDOtY81G+xslBAVHgX5WnOqqDcYkNQmr95BE7b6tc1RG 1FcQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-ext4+bounces-1692-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-ext4+bounces-1692-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=QUARANTINE) header.from=huawei.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id g10-20020a0564021eca00b0056b9ae5f94fsi397036edg.167.2024.03.19.04.33.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Mar 2024 04:33:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4+bounces-1692-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-ext4+bounces-1692-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-ext4+bounces-1692-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=QUARANTINE) header.from=huawei.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 1773D1F22E60 for ; Tue, 19 Mar 2024 11:33:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EB6537FBC2; Tue, 19 Mar 2024 11:32:19 +0000 (UTC) X-Original-To: linux-ext4@vger.kernel.org Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B7AA27F7EF; Tue, 19 Mar 2024 11:32:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.187 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710847939; cv=none; b=InQXAxysbe1iBdhnOaVX1NdMgX6g122G/ZtN3wrx+5KZLzxomTpNHXq/V2VMkDmwMlBIrcqC6wz/edIeU1Cli02MWFXhV9MuG2f9A+ohPI5hh7qYcPBH4QQ7144kmm4MWavcpjUENFbFfUty+BialdW2JpfbJbLit2qX3d659Ts= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710847939; c=relaxed/simple; bh=8AM8pym/lLuefOMlDqaQegYdsko79Al05Npk0vTE81Q=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=HAhKALGKQK+tKl3aNEwsC987FiL+2FzzVvlyOoxby0XFlgK3wrmPltesXobAh5nwqrWiol2W8nVreneQ89vYJ0hac+3KmatJNk5Vh4rvqLyLUHTC8Ca/E7V1gM51D/WBTkTBX33OwW7RJSeZySaAUPBVNIJGowTWuBmQL5x7V98= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.187 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.88.194]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4TzTyp2qlbzwPlY; Tue, 19 Mar 2024 19:29:38 +0800 (CST) Received: from dggpeml500021.china.huawei.com (unknown [7.185.36.21]) by mail.maildlp.com (Postfix) with ESMTPS id 091E7140336; Tue, 19 Mar 2024 19:32:10 +0800 (CST) Received: from huawei.com (10.175.127.227) by dggpeml500021.china.huawei.com (7.185.36.21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Tue, 19 Mar 2024 19:32:09 +0800 From: Baokun Li To: CC: , , , , , , , , , Subject: [PATCH v4 1/9] ext4: avoid overflow when setting values via sysfs Date: Tue, 19 Mar 2024 19:33:17 +0800 Message-ID: <20240319113325.3110393-2-libaokun1@huawei.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240319113325.3110393-1-libaokun1@huawei.com> References: <20240319113325.3110393-1-libaokun1@huawei.com> Precedence: bulk X-Mailing-List: linux-ext4@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpeml500021.china.huawei.com (7.185.36.21) When setting values of type unsigned int through sysfs, we use kstrtoul() to parse it and then truncate part of it as the final set value, when the set value is greater than UINT_MAX, the set value will not match what we see because of the truncation. As follows: $ echo 4294967296 > /sys/fs/ext4/sda/mb_max_linear_groups $ cat /sys/fs/ext4/sda/mb_max_linear_groups 0 So we use kstrtouint() to parse the attr_pointer_ui type to avoid the inconsistency described above. In addition, a judgment is added to avoid setting s_resv_clusters less than 0. Signed-off-by: Baokun Li Reviewed-by: Jan Kara --- fs/ext4/sysfs.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/fs/ext4/sysfs.c b/fs/ext4/sysfs.c index 6d332dff79dd..ca820620b974 100644 --- a/fs/ext4/sysfs.c +++ b/fs/ext4/sysfs.c @@ -104,7 +104,7 @@ static ssize_t reserved_clusters_store(struct ext4_sb_info *sbi, int ret; ret = kstrtoull(skip_spaces(buf), 0, &val); - if (ret || val >= clusters) + if (ret || val >= clusters || (s64)val < 0) return -EINVAL; atomic64_set(&sbi->s_resv_clusters, val); @@ -451,7 +451,8 @@ static ssize_t ext4_attr_store(struct kobject *kobj, s_kobj); struct ext4_attr *a = container_of(attr, struct ext4_attr, attr); void *ptr = calc_ptr(a, sbi); - unsigned long t; + unsigned int t; + unsigned long lt; int ret; switch (a->attr_id) { @@ -460,7 +461,7 @@ static ssize_t ext4_attr_store(struct kobject *kobj, case attr_pointer_ui: if (!ptr) return 0; - ret = kstrtoul(skip_spaces(buf), 0, &t); + ret = kstrtouint(skip_spaces(buf), 0, &t); if (ret) return ret; if (a->attr_ptr == ptr_ext4_super_block_offset) @@ -471,10 +472,10 @@ static ssize_t ext4_attr_store(struct kobject *kobj, case attr_pointer_ul: if (!ptr) return 0; - ret = kstrtoul(skip_spaces(buf), 0, &t); + ret = kstrtoul(skip_spaces(buf), 0, <); if (ret) return ret; - *((unsigned long *) ptr) = t; + *((unsigned long *) ptr) = lt; return len; case attr_inode_readahead: return inode_readahead_blks_store(sbi, buf, len); -- 2.31.1