Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp7783606ioo; Fri, 3 Jun 2022 13:29:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyEn0ZKxKPVCt1XRTiQh8LTI7T7XWYTI4dE942Bp0tL2NqZM6IXxN+t0+PHAVwb1r6L9+dN X-Received: by 2002:a17:90a:86c8:b0:1e6:7147:8b4d with SMTP id y8-20020a17090a86c800b001e671478b4dmr12577963pjv.45.1654288165606; Fri, 03 Jun 2022 13:29:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654288165; cv=none; d=google.com; s=arc-20160816; b=OMkpBTysPnT4wo2UeU8XoqIKgf4ip05NjZ5wzdEEq4RHZ+i5xRwVEpEdIf0TQuDAq2 bgtnHgiYKykxIT0pFSNqsBGKwKkToEFg0W1JzXLXe4alKbYZ8K7iiv0hF2/jRO7xD3p/ tth6tH0z3lpkDNAnwdH6/ASGlH9/3Jgmkq3ztNysMFdHPItSGP8MXuPNfsBF1vV/A5Y/ p7YsscedV1s39M65cwZcyGnVLtczfyvMu3dJBa0BMgoRN63+oRI7KYetYypy1n0kZile eKIcxfBagM17QuUGsC4qbn455n9hzr15DthSiyjihc3qkwaDe4k9culO0k+m5ZQtb/bX /Vvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=RfTNEksliZ3y2poNDmDeIKONIqonojXSfIHkatdp5wA=; b=VbfTnIWs80N9s6/52u5WUF5DaTl3+3Nn2YiZG8rBE9jB1lzgsys2tN0+WQdvZJ342A q8EdtJQVK8wchRcGkvrEBzcApLSVosq8g7cesG1r3h0VtNkZyH3rgvxT7v/ULWhLY8aE /FjKibEADN4vdyAYEAu3STckihm2aQ39Hk6yoGMRdb+GnRzP8ttUUG/XVURThQlXX9mk 0itJRxoJoNqJlfTEPXSlyd7wVR+N6PB6afY8h+A2h85LmNh19U0veoGjZ+5o663NZUGU WXqdaz1gb3N8Mh91NHDP+fw8VgKzGHxZ3KMqBf5k+w+vCVPkPUJpE9odP/YZ5pjbWxrR zhKQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=IzFzYEXu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f3-20020a635543000000b003f27cfcc9dfsi10923138pgm.828.2022.06.03.13.29.12; Fri, 03 Jun 2022 13:29:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=IzFzYEXu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237447AbiFCGkj (ORCPT + 99 others); Fri, 3 Jun 2022 02:40:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51346 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241628AbiFCGkh (ORCPT ); Fri, 3 Jun 2022 02:40:37 -0400 Received: from mail-yw1-x1132.google.com (mail-yw1-x1132.google.com [IPv6:2607:f8b0:4864:20::1132]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8215C387BD for ; Thu, 2 Jun 2022 23:40:35 -0700 (PDT) Received: by mail-yw1-x1132.google.com with SMTP id 00721157ae682-30c143c41e5so73143647b3.3 for ; Thu, 02 Jun 2022 23:40:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=RfTNEksliZ3y2poNDmDeIKONIqonojXSfIHkatdp5wA=; b=IzFzYEXuWY1GJiMU/NqMxDg9AH710M3ZuBEesd8VJGccuOl+bIht9xKv4Th4TqHSpc eWOJoiIdi/j5QAenz9PGqOifjhIfuICyi5m0hn2YYejz+TsDGSf1fFa/kyrlthrWONxq jDaHVXtfUkkYOwNB9qw/7Rj8QZRTrUcEUl76XejrlXfx62lG/x4lFnPHIbTdhD8MaOnG a9yjumLGdKWxqTkIDdkNQJdJh2OjQpuwB21L+GDVsKgYDzMDw3uTwM4xyMUQctyezlMj Du0QDBbyTJuhyjY3sunXEH9KxEdVVoTSV7oCOu77Q6Li9ncnngBEFE/e77ZujC/S7uxo ES6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=RfTNEksliZ3y2poNDmDeIKONIqonojXSfIHkatdp5wA=; b=S0kxPfl6AzUgzSCLRRlgaznYdMJOPUeYIv8z11Dk06SzkRXvHAXxvqRY0SySXMjfkw zzaP6u0Atdq/nXEih1kR62jJq5zLeNppYJ7EoB4aYLPrN/eRiozE8eVMAP/bcsxMpo0r Z0JDrmjH/GfM5JzTqk5EW+LseTfV2G6f39842RMGcCeHln7Xj+A3Qmd4kO8Z09n0z20D 4rXMa1cZ24WqJZgyrdjA6LjqWVTbIslSHgJ7PHf04v4pH9iKt/30daotlSZA/UwAQ5XD 8Gt+nPE7oldsqmftoZ+LqljppuyaZNZWnlhm3sqSsL+u5A6KwnSwefjabNl/fsV6QZep tr/A== X-Gm-Message-State: AOAM533q7L2crToSqbt5IODSNFHzRN0TyOPiLWbOLR+uGe7FOY9lBEkN SxPA40i63oPyhQ5X1mDi2dOViWgaIHHAWgAbNyuR7gfTFqqxXg== X-Received: by 2002:a81:4c8e:0:b0:300:37ba:2c1e with SMTP id z136-20020a814c8e000000b0030037ba2c1emr10333837ywa.141.1654238434452; Thu, 02 Jun 2022 23:40:34 -0700 (PDT) MIME-Version: 1.0 References: <20220525065050.38905-1-songmuchun@bytedance.com> In-Reply-To: <20220525065050.38905-1-songmuchun@bytedance.com> From: Muchun Song Date: Fri, 3 Jun 2022 14:39:58 +0800 Message-ID: Subject: Re: [PATCH v3] sysctl: handle table->maxlen robustly for proc_dobool To: LKML , linux-fsdevel Cc: Matthew Wilcox , Xiongchun duan , Luis Chamberlain , Kees Cook , Iurii Zaikin Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi all, Ping guys. Any comments or objections? On Wed, May 25, 2022 at 2:51 PM Muchun Song wrote: > > Setting ->proc_handler to proc_dobool at the same time setting ->maxlen > to sizeof(int) is counter-intuitive, it is easy to make mistakes in the > future (When I first use proc_dobool() in my driver, I assign > sizeof(variable) to table->maxlen. Then I found it was wrong, it should > be sizeof(int) which was very counter-intuitive). For robustness, > rework proc_dobool() robustly. So it is an improvement not a real bug > fix. > > Signed-off-by: Muchun Song > Cc: Luis Chamberlain > Cc: Kees Cook > Cc: Iurii Zaikin > --- > v3: > - Update commit log. > > v2: > - Reimplementing proc_dobool(). > > fs/lockd/svc.c | 2 +- > kernel/sysctl.c | 38 +++++++++++++++++++------------------- > 2 files changed, 20 insertions(+), 20 deletions(-) > > diff --git a/fs/lockd/svc.c b/fs/lockd/svc.c > index 59ef8a1f843f..6e48ee787f49 100644 > --- a/fs/lockd/svc.c > +++ b/fs/lockd/svc.c > @@ -496,7 +496,7 @@ static struct ctl_table nlm_sysctls[] = { > { > .procname = "nsm_use_hostnames", > .data = &nsm_use_hostnames, > - .maxlen = sizeof(int), > + .maxlen = sizeof(nsm_use_hostnames), > .mode = 0644, > .proc_handler = proc_dobool, > }, > diff --git a/kernel/sysctl.c b/kernel/sysctl.c > index e52b6e372c60..50a2c29efc94 100644 > --- a/kernel/sysctl.c > +++ b/kernel/sysctl.c > @@ -423,21 +423,6 @@ static void proc_put_char(void **buf, size_t *size, char c) > } > } > > -static int do_proc_dobool_conv(bool *negp, unsigned long *lvalp, > - int *valp, > - int write, void *data) > -{ > - if (write) { > - *(bool *)valp = *lvalp; > - } else { > - int val = *(bool *)valp; > - > - *lvalp = (unsigned long)val; > - *negp = false; > - } > - return 0; > -} > - > static int do_proc_dointvec_conv(bool *negp, unsigned long *lvalp, > int *valp, > int write, void *data) > @@ -708,16 +693,31 @@ int do_proc_douintvec(struct ctl_table *table, int write, > * @lenp: the size of the user buffer > * @ppos: file position > * > - * Reads/writes up to table->maxlen/sizeof(unsigned int) integer > - * values from/to the user buffer, treated as an ASCII string. > + * Reads/writes up to table->maxlen/sizeof(bool) bool values from/to > + * the user buffer, treated as an ASCII string. > * > * Returns 0 on success. > */ > int proc_dobool(struct ctl_table *table, int write, void *buffer, > size_t *lenp, loff_t *ppos) > { > - return do_proc_dointvec(table, write, buffer, lenp, ppos, > - do_proc_dobool_conv, NULL); > + struct ctl_table tmp = *table; > + bool *data = table->data; > + unsigned int val = READ_ONCE(*data); > + int ret; > + > + /* Do not support arrays yet. */ > + if (table->maxlen != sizeof(bool)) > + return -EINVAL; > + > + tmp.maxlen = sizeof(val); > + tmp.data = &val; > + ret = do_proc_douintvec(&tmp, write, buffer, lenp, ppos, NULL, NULL); > + if (ret) > + return ret; > + if (write) > + WRITE_ONCE(*data, val ? true : false); > + return 0; > } > > /** > -- > 2.11.0 >