From nobody Tue Sep 29 01:57:02 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 D544F440A0B; Thu, 13 Aug 2026 12:18:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786623530; cv=none; b=ciDl8dl7o2gb+4z6RZgvIEC058DaM8fAjincDrLOpwQdPVMwpthjmRkmM4DgTT6ghCp95f1Pe/fMacbiNpqX883uKwF8YGXJRHV6v+Ay/UCRFWKI2zJItkUxbmx4g2lSaLnChxZvLgNY9rT3Rce5CKOMfhSFLDJY6UF/L2DhndY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786623530; c=relaxed/simple; bh=9XrYzpg+oivoj10XseiG15jvZm8eJVgkb2b/7O5gaPw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=UygvqKM/KxSD2GW79GLBpSbfsnj4n8q1ZCtDGGGsXVXlDm+XGeAn/UwCP1yUJQ9yQAuQflbT2QC4/ikZiSOQ1A6aLniulVCu5zwyOVi1SbKioWi5bod+hIp7RGk4r7gB3kfpUN+z4bvclSDQoeiM0tTMzGSEMKQPczcIAZoQkWc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hrtNxndC; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="hrtNxndC" Received: by smtp.kernel.org (Postfix) with ESMTPS id 77870C2BCC7; Thu, 13 Aug 2026 12:18:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1786623530; bh=9XrYzpg+oivoj10XseiG15jvZm8eJVgkb2b/7O5gaPw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=hrtNxndCmuZW55fvrMUC5UPdgaAUBOv3gOs/i0C8JGROGgHX3YtD2evNY4Uz/c/NO 3FQt52pU+4T/YDHMA09O9v/Kiiw0mxN/PgJje7NhTxzabvC3VQqBK78JGVvCaVrplr Xa63ZAWtp3e1qZ2zmH41tR8y/08XjPsBO62RROwdFCrragt1tgmz1ryEXlMIy5taof ZS7BIsHFnQrZNXxZp1/uV7z86f3J/xOdj2MhQJoYQ9vOmXHiEiahecRWghmWCkDH3p XLFiWU56oU+Ohjzu+ZIQtKsHIFy9dGieVKqMXIukRYl6Nk27pMHOaw52uZb1BUG+Um XXk7TMNp8QhHw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 58B27C5B572; Thu, 13 Aug 2026 12:18:50 +0000 (UTC) From: Joel Granados Date: Thu, 13 Aug 2026 14:18:37 +0200 Subject: [PATCH 1/4] sysctl: Split data conversion and file position handling Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260813-lklm-partial_ctlvec-v1-1-df9e51c13704@kernel.org> References: <20260813-lklm-partial_ctlvec-v1-0-df9e51c13704@kernel.org> In-Reply-To: <20260813-lklm-partial_ctlvec-v1-0-df9e51c13704@kernel.org> To: Kees Cook , Shuah Khan , linux-mm@kvack.org Cc: Jianlin Shi , akpm@linux-foundation.org, vbabka@kernel.org, hannes@cmpxchg.org, surenb@google.com, mhocko@suse.com, jackmanb@google.com, ziy@nvidia.com, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org, Joel Granados X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=11680; i=joel.granados@kernel.org; h=from:subject:message-id; bh=9XrYzpg+oivoj10XseiG15jvZm8eJVgkb2b/7O5gaPw=; b=owJ4nAHtARL+kA0DAAoBupfNUreWQU8ByyZiAGp9tiYA9BuqtzE+LFfMYktNUKVRFQUjtN/4D 3x1aDTPCX4kd4kBswQAAQoAHRYhBK5HCVcl5jElzssnkLqXzVK3lkFPBQJqfbYmAAoJELqXzVK3 lkFPb88L+wdmpsr/Y8g1Uxge6IJMsdi1/mdjTrz4Cg3ITpeEulNUM0EkregWGeArMSK1U8yoEbi wVr0Zp5r01O3jxrnn1efE9MxLi1bvKdLw0+Y6ob2pK+1q6DKF5cJeYjhd5ZUAMuXUQlqDyCZAcy nDSUnt3ZbUAbJKFypoLDIR8Bd1IovZIPKM0sk18FKPIrmdGyorXezWFZbpEKcJtmzmjOA2zCeXR P1JvcDVUMTKV6EW1grO2x2ot1QuUrMo85ura8M7RaeVNsJ+bMjhR8SZxKt5DSIJGgQQyx9YbJqE HxBCrCspLHoHb4NwU4W4QfVEzLrqwr+LHHNsp4os3D0PNpAPygIbpc89YiYcwvLWN53h6TZxV6k fzn7oNhT57YGuSU+dPNGQ6yAjc1yEu8aqk+vgkPNQ2uKZ7qPo2hKshbUgDituPMEA89xHaYpX70 2gFxq9yjSmjUH5Dzvw6W/8pgkMMVwL1XozkicAvDKKpUPcCNMNNeORDpDWcpxy1Ez0BuQP/fK/6 TY= X-Developer-Key: i=joel.granados@kernel.org; a=openpgp; fpr=F1F8E46D30F0F6C4A45FF4465895FAAC338C6E77 X-Endpoint-Received: by B4 Relay for joel.granados@kernel.org/default with auth_id=239 Apply the conversions to the data in a new helper function (apply_conv_on_vec) while file position handling and argument validation stay in the original function. Rename function to prov_vec (from do_proc_vec). This is a prep commit to isolate the logic that needs to change to prevent partial sysctl vector writes. No functional change intended Signed-off-by: Joel Granados --- kernel/sysctl.c | 202 ++++++++++++++++++++++++++++++++--------------------= ---- 1 file changed, 117 insertions(+), 85 deletions(-) diff --git a/kernel/sysctl.c b/kernel/sysctl.c index f7b75985d5420a522a316d1fade40dc7fbe60455..ed0e5101949c2fa56e33d543c65= 175d0ab579fc7 100644 --- a/kernel/sysctl.c +++ b/kernel/sysctl.c @@ -595,7 +595,7 @@ static int do_proc_int_conv_minmax(bool *negp, unsigned= long *u_ptr, int *k_ptr, static const char proc_wspace_sep[] =3D { ' ', '\t', '\n' }; =20 /* - * Element type processed by do_proc_vec(). The tag selects the element si= ze + * Element type processed by proc_vec(). The tag selects the element size * and signedness, and it selects which member of union proc_vec_conv is l= ive. */ enum proc_vec_type { @@ -605,7 +605,7 @@ enum proc_vec_type { }; =20 /* - * Converter passed to do_proc_vec(). Only the member matching the + * Converter passed to proc_vec(). Only the member matching the * enum proc_vec_type tag is ever read, so every dispatch stays fully type= d and * no void * converter pointer is needed. */ @@ -637,101 +637,133 @@ static int proc_vec_conv(enum proc_vec_type type, u= nion proc_vec_conv conv, return -EINVAL; } =20 -/* - * Read/write a vector of @type elements. The element size and signedness = are - * derived from @type, so a single runtime function replaces the per-type - * variants. table->data is walked as raw bytes (@i) advanced by @size; the - * converter performs the actual typed load/store. +/** + * apply_conv_on_vec - Apply converter function on data vector + * + * Element signedness is derived from @data_type. table->data is walked + * as raw bytes (@data) advanced by @data_size; the converter performs + * the actual typed load/store. Sets buf_left_final to the number of + * bytes that where left outstanding after conversion. Can be > 0. + * + * Returns 0 on success. Non-zero on error. */ -static int do_proc_vec(const struct ctl_table *table, int dir, - void *buffer, size_t *lenp, loff_t *ppos, - enum proc_vec_type type, union proc_vec_conv conv) +static int apply_conv_on_vec(const union proc_vec_conv conv, + const struct ctl_table *table, + const enum proc_vec_type data_type, + const size_t data_size, const int conv_dir, + const size_t buf_nbyte, void *buf, + size_t *buf_left_final) { - int vleft, first =3D 1, err =3D 0; - size_t left, size; - bool is_unsigned; - char *i, *p; + int vec_left, first =3D 1, err =3D 0; + size_t buf_left; + char *data, *p; + bool is_unsigned =3D data_type =3D=3D PROC_VEC_UINT || data_type =3D=3D P= ROC_VEC_ULONG; =20 - switch (type) { - case PROC_VEC_INT: - size =3D sizeof(int); - is_unsigned =3D false; - break; - case PROC_VEC_UINT: - size =3D sizeof(uint); - is_unsigned =3D true; - break; - case PROC_VEC_ULONG: - size =3D sizeof(ulong); - is_unsigned =3D true; - break; - default: - return -EINVAL; - } - - if (!table->data || !table->maxlen || !*lenp || - (*ppos && SYSCTL_KERN_TO_USER(dir))) { - *lenp =3D 0; - return 0; - } - - i =3D table->data; - vleft =3D table->maxlen / size; - left =3D *lenp; - - /* uint arrays are not supported, *Do not* add support for them. */ - if (type =3D=3D PROC_VEC_UINT && vleft !=3D 1) - return -EINVAL; - - if (SYSCTL_USER_TO_KERN(dir)) { - if (proc_first_pos_non_zero_ignore(ppos, table)) - goto out; + data =3D table->data; + vec_left =3D table->maxlen / data_size; + buf_left =3D buf_nbyte; =20 - if (left > PAGE_SIZE - 1) - left =3D PAGE_SIZE - 1; - p =3D buffer; + if (SYSCTL_USER_TO_KERN(conv_dir)) { + if (buf_left > PAGE_SIZE - 1) + buf_left =3D PAGE_SIZE - 1; + p =3D buf; } =20 - for (; left && vleft--; i +=3D size, first =3D 0) { + for (; buf_left && vec_left--; data +=3D data_size, first =3D 0) { unsigned long lval; bool neg =3D false; =20 - if (SYSCTL_USER_TO_KERN(dir)) { - proc_skip_spaces(&p, &left); + if (SYSCTL_USER_TO_KERN(conv_dir)) { + proc_skip_spaces(&p, &buf_left); =20 - if (!left) + if (!buf_left) break; - err =3D proc_get_long(&p, &left, &lval, &neg, + err =3D proc_get_long(&p, &buf_left, &lval, &neg, proc_wspace_sep, sizeof(proc_wspace_sep), NULL); if (!err && neg && is_unsigned) err =3D -EINVAL; if (err) break; - if (proc_vec_conv(type, conv, &neg, &lval, i, dir, table)) { + if (proc_vec_conv(data_type, conv, &neg, &lval, data, conv_dir, table))= { err =3D -EINVAL; break; } } else { - if (proc_vec_conv(type, conv, &neg, &lval, i, dir, table)) { + if (proc_vec_conv(data_type, conv, &neg, &lval, data, conv_dir, table))= { err =3D -EINVAL; break; } if (!first) - proc_put_char(&buffer, &left, '\t'); - proc_put_long(&buffer, &left, lval, neg); + proc_put_char(&buf, &buf_left, '\t'); + proc_put_long(&buf, &buf_left, lval, neg); } } =20 - if (SYSCTL_KERN_TO_USER(dir) && !first && left && !err) - proc_put_char(&buffer, &left, '\n'); - if (SYSCTL_USER_TO_KERN(dir) && !err && left) - proc_skip_spaces(&p, &left); - if (SYSCTL_USER_TO_KERN(dir) && first) + if (SYSCTL_KERN_TO_USER(conv_dir) && !first && buf_left && !err) + proc_put_char(&buf, &buf_left, '\n'); + if (SYSCTL_USER_TO_KERN(conv_dir) && !err && buf_left) + proc_skip_spaces(&p, &buf_left); + if (SYSCTL_USER_TO_KERN(conv_dir) && first) return err ? : -EINVAL; - *lenp -=3D left; + *buf_left_final =3D buf_left; + + return err; +} + +/* Read/write a vector of @type elements. */ +static int proc_vec(const struct ctl_table *table, int dir, void *buffer, + size_t *lenp, loff_t *ppos, enum proc_vec_type type, + union proc_vec_conv conv) +{ + int err =3D 0; + size_t data_size, left_nbyte =3D SIZE_MAX; + + switch (type) { + case PROC_VEC_INT: + data_size =3D sizeof(int); + break; + case PROC_VEC_UINT: + data_size =3D sizeof(uint); + break; + case PROC_VEC_ULONG: + data_size =3D sizeof(ulong); + break; + default: + return -EINVAL; + } + + if (!table->data || !table->maxlen || !*lenp || + (*ppos && SYSCTL_KERN_TO_USER(dir))) { + *lenp =3D 0; + return 0; + } + + /* uint arrays are not supported, *Do not* add support for them. */ + if (type =3D=3D PROC_VEC_UINT && (table->maxlen / data_size) !=3D 1) + return -EINVAL; + + if (SYSCTL_USER_TO_KERN(dir)) { + if (proc_first_pos_non_zero_ignore(ppos, table)) + goto out; + } + + err =3D apply_conv_on_vec(conv, table, type, data_size, dir, *lenp, buffe= r, + &left_nbyte); + + /* + * An unchanged left_nbyte signals a write with no parsed element; which + * is an error. Using SIZE_MAX to detect this error is possible because: + * 1. lenp is bounded by KMALLOC_MAX_SIZE in proc_sys_call_handler + * 2. lenp could never be SIZE_MAX as it is a "ridiculous" (exabyte) allo= cation. + */ + if (left_nbyte =3D=3D SIZE_MAX) + return err; + + *lenp -=3D left_nbyte; out: *ppos +=3D *lenp; + return err; } =20 @@ -760,8 +792,8 @@ int proc_douintvec_conv(const struct ctl_table *table, = int dir, void *buffer, if (!conv) conv =3D do_proc_uint_conv; =20 - return do_proc_vec(table, dir, buffer, lenp, ppos, PROC_VEC_UINT, - (union proc_vec_conv){ .uint_conv =3D conv }); + return proc_vec(table, dir, buffer, lenp, ppos, PROC_VEC_UINT, + (union proc_vec_conv){ .uint_conv =3D conv }); } =20 /** @@ -820,8 +852,8 @@ int proc_dobool(const struct ctl_table *table, int dir,= void *buffer, int proc_dointvec(const struct ctl_table *table, int dir, void *buffer, size_t *lenp, loff_t *ppos) { - return do_proc_vec(table, dir, buffer, lenp, ppos, PROC_VEC_INT, - (union proc_vec_conv){ .int_conv =3D do_proc_int_conv }); + return proc_vec(table, dir, buffer, lenp, ppos, PROC_VEC_INT, + (union proc_vec_conv){ .int_conv =3D do_proc_int_conv }); } =20 /** @@ -840,8 +872,8 @@ int proc_dointvec(const struct ctl_table *table, int di= r, void *buffer, int proc_douintvec(const struct ctl_table *table, int dir, void *buffer, size_t *lenp, loff_t *ppos) { - return do_proc_vec(table, dir, buffer, lenp, ppos, PROC_VEC_UINT, - (union proc_vec_conv){ .uint_conv =3D do_proc_uint_conv }); + return proc_vec(table, dir, buffer, lenp, ppos, PROC_VEC_UINT, + (union proc_vec_conv){ .uint_conv =3D do_proc_uint_conv }); } =20 /** @@ -864,8 +896,8 @@ int proc_douintvec(const struct ctl_table *table, int d= ir, void *buffer, int proc_dointvec_minmax(const struct ctl_table *table, int dir, void *buffer, size_t *lenp, loff_t *ppos) { - return do_proc_vec(table, dir, buffer, lenp, ppos, PROC_VEC_INT, - (union proc_vec_conv){ .int_conv =3D do_proc_int_conv_minmax }); + return proc_vec(table, dir, buffer, lenp, ppos, PROC_VEC_INT, + (union proc_vec_conv){ .int_conv =3D do_proc_int_conv_minmax }); } =20 /** @@ -891,8 +923,8 @@ int proc_dointvec_minmax(const struct ctl_table *table,= int dir, int proc_douintvec_minmax(const struct ctl_table *table, int dir, void *buffer, size_t *lenp, loff_t *ppos) { - return do_proc_vec(table, dir, buffer, lenp, ppos, PROC_VEC_UINT, - (union proc_vec_conv){ .uint_conv =3D do_proc_uint_conv_minmax }); + return proc_vec(table, dir, buffer, lenp, ppos, PROC_VEC_UINT, + (union proc_vec_conv){ .uint_conv =3D do_proc_uint_conv_minmax }); } =20 /** @@ -935,8 +967,8 @@ int proc_dou8vec_minmax(const struct ctl_table *table, = int dir, tmp.extra2 =3D (unsigned int *) &max; =20 val =3D READ_ONCE(*data); - res =3D do_proc_vec(&tmp, dir, buffer, lenp, ppos, PROC_VEC_UINT, - (union proc_vec_conv){ .uint_conv =3D do_proc_uint_conv_minmax }); + res =3D proc_vec(&tmp, dir, buffer, lenp, ppos, PROC_VEC_UINT, + (union proc_vec_conv){ .uint_conv =3D do_proc_uint_conv_minmax }); if (res) return res; if (SYSCTL_USER_TO_KERN(dir)) @@ -1066,8 +1098,8 @@ int proc_doulongvec_conv(const struct ctl_table *tabl= e, int dir, int (*conv)(bool *negp, ulong *u_ptr, ulong *k_ptr, int dir, const struct ctl_table *table)) { - return do_proc_vec(table, dir, buffer, lenp, ppos, PROC_VEC_ULONG, - (union proc_vec_conv){ .ulong_conv =3D conv }); + return proc_vec(table, dir, buffer, lenp, ppos, PROC_VEC_ULONG, + (union proc_vec_conv){ .ulong_conv =3D conv }); } =20 /** @@ -1087,10 +1119,10 @@ int proc_doulongvec_conv(const struct ctl_table *ta= ble, int dir, * Returns: %0 on success. */ int proc_doulongvec_minmax(const struct ctl_table *table, int dir, - void *buffer, size_t *lenp, loff_t *ppos) + void *buffer, size_t *lenp, loff_t *ppos) { - return do_proc_vec(table, dir, buffer, lenp, ppos, PROC_VEC_ULONG, - (union proc_vec_conv){ .ulong_conv =3D do_proc_ulong_conv }); + return proc_vec(table, dir, buffer, lenp, ppos, PROC_VEC_ULONG, + (union proc_vec_conv){ .ulong_conv =3D do_proc_ulong_conv }); } =20 /** @@ -1114,8 +1146,8 @@ int proc_dointvec_conv(const struct ctl_table *table,= int dir, void *buffer, { if (!conv) conv =3D do_proc_int_conv; - return do_proc_vec(table, dir, buffer, lenp, ppos, PROC_VEC_INT, - (union proc_vec_conv){ .int_conv =3D conv }); + return proc_vec(table, dir, buffer, lenp, ppos, PROC_VEC_INT, + (union proc_vec_conv){ .int_conv =3D conv }); } =20 /** --=20 2.50.1 From nobody Tue Sep 29 01:57:02 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 D522834A791; Thu, 13 Aug 2026 12:18:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786623530; cv=none; b=HCcqDuvZqtLL4QESG0SdezAN1avw4YZ36qvE0q9NbFp3qe0xuU274bUtTBd//8uMOQ/otPzP963RY7X+V0zYREZrdbTg0mL/UQsS9evfDO5QKjh10NMOfDdwNkbfwZJMnkBRfQJmCmW5I+bjqoo0Qcrt7wb1TxpjXATKl8Q5+Tc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786623530; c=relaxed/simple; bh=yv5GjkliXQcT8cxSUVRpuQJjqp7Lbh33VFnF+Gh4c50=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Ntu2oCM9SUPJ1/JHiXRskZSAKcnCH2/JB0IxQFVVtCNOXlmHCm1mS1zQLqlMZZrMTNVPWaj7kEVzBASfBR5BgT5NuzPFOwiqxtK4MgQ9RxCDNQ4EV7ZvYQ/5MVYduK36ADnenWLxG9M/K1dsO6QxoYS/qt93fjsIKYVP+ytmrNQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ryh/evkH; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ryh/evkH" Received: by smtp.kernel.org (Postfix) with ESMTPS id 82A97C2BCF7; Thu, 13 Aug 2026 12:18:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1786623530; bh=yv5GjkliXQcT8cxSUVRpuQJjqp7Lbh33VFnF+Gh4c50=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=ryh/evkH2yYzjBBuy21AYS+pDPgkDR7PtJFHkNohqaG/5I1IoUIUXENrzHuOiuUEs CfoBa0BfJ787c6yAd6D2kOx3s0gJHibcViqsR4Uw5IMnwUJrn6oRYUNI4ReHu1D6Au qC8JIVm+uGAtRanKedYNuTkPDaWsOQwxdCrIf01Yd7BcZhCBBffXteBKQSQx+bXoSL mG3AEmzOG4hADhzy12tzS6T1jJfUAqhE0Dt1Vos5jn/GLDp74IICZ8oFj1likVT7lJ 09FWrQOABaK9+moLPDLw3p9cVVciLDEHY59vYgPXWrBP8S1qQMF3uuQEVpzxZnJ4bZ dJRF81Vir284Q== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 647F5C5DF66; Thu, 13 Aug 2026 12:18:50 +0000 (UTC) From: Joel Granados Date: Thu, 13 Aug 2026 14:18:38 +0200 Subject: [PATCH 2/4] sysctl: Reject uint arrays before calling the general proc_vec Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260813-lklm-partial_ctlvec-v1-2-df9e51c13704@kernel.org> References: <20260813-lklm-partial_ctlvec-v1-0-df9e51c13704@kernel.org> In-Reply-To: <20260813-lklm-partial_ctlvec-v1-0-df9e51c13704@kernel.org> To: Kees Cook , Shuah Khan , linux-mm@kvack.org Cc: Jianlin Shi , akpm@linux-foundation.org, vbabka@kernel.org, hannes@cmpxchg.org, surenb@google.com, mhocko@suse.com, jackmanb@google.com, ziy@nvidia.com, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org, Joel Granados X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=2851; i=joel.granados@kernel.org; h=from:subject:message-id; bh=yv5GjkliXQcT8cxSUVRpuQJjqp7Lbh33VFnF+Gh4c50=; b=owJ4nAHtARL+kA0DAAoBupfNUreWQU8ByyZiAGp9tievBOA8miXa3XTlhlLBnnybuWYQGNvc0 nRW5nZVG3g0RYkBswQAAQoAHRYhBK5HCVcl5jElzssnkLqXzVK3lkFPBQJqfbYnAAoJELqXzVK3 lkFPWHgL/3GOSaXYdmehuwFyZyAqE0pIMfSa1A0ISRSPCn80Dm1XIZm1UJz+XrXAaYqjqbSVbYW mDb1Ukt/LYEaKpoqJqu3I10CKPMQObYQrXuWZuSHYfZ6t/jSZhjC5gf9tfberLK2Secv7/3UrWA WH1/ot+xDko9X8ldQxC4xNjuJT9ItUriOdvmeWuvZ7LXTAxspY7kZLZkOD3B2RLKhnDBmsgWJeB h1gvq5wBnVS0BCAEMEIRAUy9WyGZ4aM0K7zygMTmucBsOvaeHGVIIeFwglgUImcxtbiGD2W5E4v QPDTxy9xLWi5OAMFFqUrSHZCsEX3ZsxBIZ2dL7tH5wQ6ccXfPGSYmTQXv+LassO+RUGJ9Zj6p1N gWsnm5FlrpPLimv6MDfpvvNYT8oR0gn9AeZ4iq2UayFCBGSBvNu+PhPDvNW1AuW8OdlqvK2dUx+ eiUf2cUTD4IM3a2vyeTdhBaqE0N+s2Dh5xBXdVsteIc1eEmdJA3uP5mRyHlPCXbZrJRlcs+Su87 Uo= X-Developer-Key: i=joel.granados@kernel.org; a=openpgp; fpr=F1F8E46D30F0F6C4A45FF4465895FAAC338C6E77 X-Endpoint-Received: by B4 Relay for joel.granados@kernel.org/default with auth_id=239 Move the UINT vector size check to proc_douintvec_conv; the function that routes UINT types only. Route all the UINT calls (including proc_dou8vec_minmax) through proc_douintvec_conv. UINT proc handlers that incorrectly define maxlen will now return -EINVAL instead of 0 in the cases where data is missing, lenp is 0 or ppos is 0. Note that maxlen =3D=3D 0 is not considered as miss-defined. Signed-off-by: Joel Granados --- kernel/sysctl.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/kernel/sysctl.c b/kernel/sysctl.c index ed0e5101949c2fa56e33d543c65175d0ab579fc7..c5fa916e626a336c004d596f4c7= 4f829b1cdc5e1 100644 --- a/kernel/sysctl.c +++ b/kernel/sysctl.c @@ -739,10 +739,6 @@ static int proc_vec(const struct ctl_table *table, int= dir, void *buffer, return 0; } =20 - /* uint arrays are not supported, *Do not* add support for them. */ - if (type =3D=3D PROC_VEC_UINT && (table->maxlen / data_size) !=3D 1) - return -EINVAL; - if (SYSCTL_USER_TO_KERN(dir)) { if (proc_first_pos_non_zero_ignore(ppos, table)) goto out; @@ -788,6 +784,9 @@ int proc_douintvec_conv(const struct ctl_table *table, = int dir, void *buffer, int (*conv)(bool *negp, ulong *u_ptr, uint *k_ptr, int dir, const struct ctl_table *table)) { + /* uint arrays are not supported, *Do not* add support for them. */ + if (table->maxlen && (table->maxlen / sizeof(uint)) !=3D 1) + return -EINVAL; =20 if (!conv) conv =3D do_proc_uint_conv; @@ -872,8 +871,7 @@ int proc_dointvec(const struct ctl_table *table, int di= r, void *buffer, int proc_douintvec(const struct ctl_table *table, int dir, void *buffer, size_t *lenp, loff_t *ppos) { - return proc_vec(table, dir, buffer, lenp, ppos, PROC_VEC_UINT, - (union proc_vec_conv){ .uint_conv =3D do_proc_uint_conv }); + return proc_douintvec_conv(table, dir, buffer, lenp, ppos, do_proc_uint_c= onv); } =20 /** @@ -923,8 +921,8 @@ int proc_dointvec_minmax(const struct ctl_table *table,= int dir, int proc_douintvec_minmax(const struct ctl_table *table, int dir, void *buffer, size_t *lenp, loff_t *ppos) { - return proc_vec(table, dir, buffer, lenp, ppos, PROC_VEC_UINT, - (union proc_vec_conv){ .uint_conv =3D do_proc_uint_conv_minmax }); + return proc_douintvec_conv(table, dir, buffer, lenp, ppos, + do_proc_uint_conv_minmax); } =20 /** @@ -967,8 +965,7 @@ int proc_dou8vec_minmax(const struct ctl_table *table, = int dir, tmp.extra2 =3D (unsigned int *) &max; =20 val =3D READ_ONCE(*data); - res =3D proc_vec(&tmp, dir, buffer, lenp, ppos, PROC_VEC_UINT, - (union proc_vec_conv){ .uint_conv =3D do_proc_uint_conv_minmax }); + res =3D proc_douintvec_minmax(&tmp, dir, buffer, lenp, ppos); if (res) return res; if (SYSCTL_USER_TO_KERN(dir)) --=20 2.50.1 From nobody Tue Sep 29 01:57:02 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 D5366416852; Thu, 13 Aug 2026 12:18:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786623530; cv=none; b=eKY2zcbicfp94vHubGlOjuqE8xK6aAzVkWy99DL2Zp/EQj3IgBcd1uUCbAKtIJysARcJSR7/ZqdNZMv94PSsWDG2gPziUj3pZv8VKKSnW2TCyzPVhT3MQsgq+klGHuCLX25HNjSeeDYWc9KIEc9AEprbx2kPpJb9hqmyzeWxF9g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786623530; c=relaxed/simple; bh=RQxbygSuwuyad2h3rnwJdX+5/FDgwJkAzThNIKfsnKw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=jl8tGU4TtvYn8aQnETdvEkKn5QodTdwxJBD9A2JkrnFFL4KqKqKbvppKCKhwSQv6EtAJI/Ycl3zp2FHRR0dkMvR7LoDv/LWArcrRxmN6EN/yibc2Pry2ClFCHrQPeAzrGj6UMVO258zCTp2uoWGwDPv9E3d+1tXxC2rjoqeblXM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Wkb8wCBH; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Wkb8wCBH" Received: by smtp.kernel.org (Postfix) with ESMTPS id 903E7C2BCFA; Thu, 13 Aug 2026 12:18:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1786623530; bh=RQxbygSuwuyad2h3rnwJdX+5/FDgwJkAzThNIKfsnKw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Wkb8wCBHVyP7hmrq29NCqd3BCfuAro9J22h1l7HqmDzflqEPhjAiUVuztmjDGGai7 Nt9/Al03eqBCZtffr4YFe2pRjCRm2dIhRO0kjcjwEMTX46makDPB6LPf8E08lnwoc7 VoW72KTwhh4o8dqQj3D+Z16wqlfxrxeK362wZQdumu6/35iN0RPL9zpLgPY8uJAmnx N1+rXsDAQCR5e5wGXlbN5CtdZ/cyMNpcD10eDsT2bsjaYjdRtPooMuJo8UQOMJOOdR M2+nAuMNtJOUe/cv0Jc+VtFnOhcXSyDlgJGpyMq0lYQSfiEFk1U4KR0JKuVVI8wvDH Je/kAy+K166jQ== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 70EC7C5CFCF; Thu, 13 Aug 2026 12:18:50 +0000 (UTC) From: Joel Granados Date: Thu, 13 Aug 2026 14:18:39 +0200 Subject: [PATCH 3/4] sysctl: Disallow partial updates for erroneous sysctl vectors Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260813-lklm-partial_ctlvec-v1-3-df9e51c13704@kernel.org> References: <20260813-lklm-partial_ctlvec-v1-0-df9e51c13704@kernel.org> In-Reply-To: <20260813-lklm-partial_ctlvec-v1-0-df9e51c13704@kernel.org> To: Kees Cook , Shuah Khan , linux-mm@kvack.org Cc: Jianlin Shi , akpm@linux-foundation.org, vbabka@kernel.org, hannes@cmpxchg.org, surenb@google.com, mhocko@suse.com, jackmanb@google.com, ziy@nvidia.com, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org, Joel Granados X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=4178; i=joel.granados@kernel.org; h=from:subject:message-id; bh=RQxbygSuwuyad2h3rnwJdX+5/FDgwJkAzThNIKfsnKw=; b=owJ4nAHtARL+kA0DAAoBupfNUreWQU8ByyZiAGp9tidSYtNXQW85HJlqyfka+JzTA3W1ID/P+ //JCQ9gVdu7kIkBswQAAQoAHRYhBK5HCVcl5jElzssnkLqXzVK3lkFPBQJqfbYnAAoJELqXzVK3 lkFPrKsMAJAzfNd9gpRVn5Z31OoG1SWQsmpB08hMmYsLfLwkc/lQvgoZPRcjQTTTRuqSOhCFKdX K6x65TcNfs6csSK5kjZhwTQ95EQeEKsV7Rz3y6NAq6eajw568s6sdAzmCHTn3LHR+Fgm7IaUF29 i5mI9AN93rogslFVW324J3b0wLgV/Lz4GgNRjeoGfuU/iWcAY/aOTHOXpNqsUSM3oeC4iyi2cDw xTt8wnM+2TN+REMFCe//9jJOTSSuh7OXLCxKlFVdxk+RQZfz+fDME/93ccuLETuW915qQzjriKu 5tMlt56XSXUyciuU6ecNHBhlQBU20VvJ0fDpqwbs1Rg7cPXdQ3w+hKTS2ncbVBrhjl2NyeIkP28 lt/n2BiyVqANSfOTwhkF1C7WsaMeI9xY00Dl5yHbzpPVqLuA/CGnWOAEdDsu/q43Mndga/89Ov7 vfO2pcxsVB5CBYSWgpqS+av6TDfhJklrItyjulhjUkUcUkqrdYjDcA4i8WGjqf+D3S2XvVSxQ/5 iQ= X-Developer-Key: i=joel.granados@kernel.org; a=openpgp; fpr=F1F8E46D30F0F6C4A45FF4465895FAAC338C6E77 X-Endpoint-Received: by B4 Relay for joel.granados@kernel.org/default with auth_id=239 When updating the kernel sysctl vectors there is a chance that not all vector elements are updated due to erroneous input. Use a staging variable that holds a copy of the vector and commits to the actual table->data only when all input is successfully updated. The staging is only for vectors; cases where table->data points to a variable should not be staged as they will not be updated on input error. PROC_VEC_UINT is not included because UINT arrays are not allowed. Replace first with nr_conv, incremented where first was cleared. first is exactly nr_conv =3D=3D 0, and the counter doubles as the number of elements to publish. Example of behavior that is being prevented: # echo "4 4 1 7" > /proc/sys/kernel/printk # echo "1 x" > /proc/sys/kernel/printk -bash: echo: write error: Invalid argument # cat /proc/sys/kernel/printk 1 4 1 7 <- incorrect It should be unchanged ("4 4 1 7") on error. Link: https://lore.kernel.org/all/tencent_A860C873956A52E26AD8D309A308A241B= A08@qq.com/ Signed-off-by: Joel Granados --- kernel/sysctl.c | 65 +++++++++++++++++++++++++++++++++++++++++++++--------= ---- 1 file changed, 52 insertions(+), 13 deletions(-) diff --git a/kernel/sysctl.c b/kernel/sysctl.c index c5fa916e626a336c004d596f4c74f829b1cdc5e1..787f53d70507b1583518f86d50d= 526828462a902 100644 --- a/kernel/sysctl.c +++ b/kernel/sysctl.c @@ -637,6 +637,26 @@ static int proc_vec_conv(enum proc_vec_type type, unio= n proc_vec_conv conv, return -EINVAL; } =20 +static int commit_conv_vec(const enum proc_vec_type data_type, void *dst, + const void *src, size_t nr) +{ + size_t i; + + switch (data_type) { + case PROC_VEC_INT: + for (i =3D 0; i < nr; i++) + WRITE_ONCE(((int *)dst)[i], ((const int *)src)[i]); + return 0; + + case PROC_VEC_ULONG: + for (i =3D 0; i < nr; i++) + WRITE_ONCE(((ulong *)dst)[i], ((const ulong *)src)[i]); + return 0; + default: + return -EINVAL; + } +} + /** * apply_conv_on_vec - Apply converter function on data vector * @@ -654,22 +674,31 @@ static int apply_conv_on_vec(const union proc_vec_con= v conv, const size_t buf_nbyte, void *buf, size_t *buf_left_final) { - int vec_left, first =3D 1, err =3D 0; - size_t buf_left; - char *data, *p; + int vec_left, err =3D 0; + size_t buf_left, nr_conv =3D 0; + char *data, *data_stage =3D NULL, *p; bool is_unsigned =3D data_type =3D=3D PROC_VEC_UINT || data_type =3D=3D P= ROC_VEC_ULONG; =20 - data =3D table->data; - vec_left =3D table->maxlen / data_size; buf_left =3D buf_nbyte; =20 + data =3D table->data; if (SYSCTL_USER_TO_KERN(conv_dir)) { if (buf_left > PAGE_SIZE - 1) buf_left =3D PAGE_SIZE - 1; p =3D buf; + + if (table->maxlen > data_size) { + data_stage =3D kmemdup(table->data, table->maxlen, GFP_KERNEL); + if (!data_stage) { + err =3D -ENOMEM; + goto out; + } + data =3D data_stage; + } } =20 - for (; buf_left && vec_left--; data +=3D data_size, first =3D 0) { + vec_left =3D table->maxlen / data_size; + for (; buf_left && vec_left--; data +=3D data_size, nr_conv++) { unsigned long lval; bool neg =3D false; =20 @@ -694,20 +723,30 @@ static int apply_conv_on_vec(const union proc_vec_con= v conv, err =3D -EINVAL; break; } - if (!first) + if (nr_conv) proc_put_char(&buf, &buf_left, '\t'); proc_put_long(&buf, &buf_left, lval, neg); } } =20 - if (SYSCTL_KERN_TO_USER(conv_dir) && !first && buf_left && !err) - proc_put_char(&buf, &buf_left, '\n'); - if (SYSCTL_USER_TO_KERN(conv_dir) && !err && buf_left) - proc_skip_spaces(&p, &buf_left); - if (SYSCTL_USER_TO_KERN(conv_dir) && first) - return err ? : -EINVAL; + if (SYSCTL_USER_TO_KERN(conv_dir)) { + if (!err && buf_left) + proc_skip_spaces(&p, &buf_left); + if (!nr_conv) { + err =3D err ? : -EINVAL; + goto out; + } + if (!err && data_stage) + err =3D commit_conv_vec(data_type, table->data, data_stage, nr_conv); + } else { + if (nr_conv && buf_left && !err) + proc_put_char(&buf, &buf_left, '\n'); + } + *buf_left_final =3D buf_left; =20 +out: + kfree(data_stage); return err; } =20 --=20 2.50.1 From nobody Tue Sep 29 01:57:02 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 ECA16442FC2; Thu, 13 Aug 2026 12:18:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786623531; cv=none; b=HUX64TR4/n8IDh3dLKKaBls2KbUd6LaSddw85tOzP+dBJUgmdOxo7Xz1IHYmlN91E14JOwACnBT87ORLOjeP57MCQ0+OEO5k0D/XMBuJWobLD7AhppAnETPQAe/TjeoPeIIWCDCdq17/r81KHaj3CLJCUGiCSwx0Bb9oTyLtofI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786623531; c=relaxed/simple; bh=bx1auWMjtQ6lK77Ub+fPqi3tw5TQNFTmgBgtSoVMeRE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=KymYpLe8e84czeeITg8/sgs/NzXcV1eJgYi3de1G755yyeM3ELkwCyT52+8btN3Aq9dkHr3kslb1WUOaCdrkQqSLOzzy6mpfM0IxkuaTIcdz10AKdYq4t9AjgDfC7QoCR/bKXDeYqpxhFOj8FTXqKRloWRMwDtmhEmPfGNqGpDo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=LYpj97qf; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="LYpj97qf" Received: by smtp.kernel.org (Postfix) with ESMTPS id 9A813C2BCF5; Thu, 13 Aug 2026 12:18:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1786623530; bh=bx1auWMjtQ6lK77Ub+fPqi3tw5TQNFTmgBgtSoVMeRE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=LYpj97qfq8/4uunncx8KuPwgfOXjs0rw8uGLRmuA0F2V3UcGUpxgxD4AKwel7bz1d DMtFMIyBQBIl+0Pfn+WkV3cwPWGM2uCVrKUlLLSmo4ROy1rXWkLyyh7ZJSLbjfTBm9 tJDRiGXg/UH0cBGqHA+r1tIk2ZYAke6omkHZ3TiwqukEjbXq0r0Ezp+YFnTKsH/kad Xnilsn2tzu8BwXKEfEEdXE3PN1U3bEf31Hhgb4X8letqE2utERAgGDdLTPUW/fa7sX ek38L6kKMGFSakv9HEHup+yqs8rJ5hy9OLhXJRHj7NwxCi1vnKpB0+MN8CzQy96WwW e1sL7uNXBh+8w== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7D3D6C5DF69; Thu, 13 Aug 2026 12:18:50 +0000 (UTC) From: Joel Granados Date: Thu, 13 Aug 2026 14:18:40 +0200 Subject: [PATCH 4/4] sysctl: Add 0013 to test partially updated vectors Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260813-lklm-partial_ctlvec-v1-4-df9e51c13704@kernel.org> References: <20260813-lklm-partial_ctlvec-v1-0-df9e51c13704@kernel.org> In-Reply-To: <20260813-lklm-partial_ctlvec-v1-0-df9e51c13704@kernel.org> To: Kees Cook , Shuah Khan , linux-mm@kvack.org Cc: Jianlin Shi , akpm@linux-foundation.org, vbabka@kernel.org, hannes@cmpxchg.org, surenb@google.com, mhocko@suse.com, jackmanb@google.com, ziy@nvidia.com, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org, Joel Granados X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=3132; i=joel.granados@kernel.org; h=from:subject:message-id; bh=bx1auWMjtQ6lK77Ub+fPqi3tw5TQNFTmgBgtSoVMeRE=; b=owJ4nAHtARL+kA0DAAoBupfNUreWQU8ByyZiAGp9tigf0HZ/SOSipnWglbugNlaMER9isn8Ln HKjGsYXnp8SHIkBswQAAQoAHRYhBK5HCVcl5jElzssnkLqXzVK3lkFPBQJqfbYoAAoJELqXzVK3 lkFPa8gL+wVFctfUarU/BECGG+yem97ZpueVfSLV32B2PJJKmFL7c6BeArL3+nFK66TgrYUfEDI 794BeoxGMD2zXrMWvkrt+ynKROFyKvwTTEiUXIy/2RLYaYWMfIHBlDpFhIlgxYOk+JRBGtZ9lCT jKEGmr8rUvShwgJ79bMF4GiJ2DyCL+FcGIA4ZZVAsYkaYocp3ryyn25z6J1zzanLyqnGBayYCW7 4RmOuwTSD6qkmUE5kh9XezhlQhbYW639iHxhLnDmGS4tQ/nIiLrXIVTUdK5L7mVI8ikNwgz+Kz7 /r4G32tJLbnV0q3aDiA4L7tBUUWlWO9/0vAtXw52FtmR5APUyDHPRntB9K9XN/IV5EZFxiSIhNe QhRZ/0aJjRBQf9lgKmyd2kTFs7VHY1gLNnJW0URRuJeCryne0K3vqm+zKpnwFs59ZLD8wPftoYZ MaG77XNioStWHAwaTevL7oRdTTv+C+DNryBbdYAxsSZcc0CC+0oew2BnXWGTWrnjwlzthSD8Vs4 +k= X-Developer-Key: i=joel.granados@kernel.org; a=openpgp; fpr=F1F8E46D30F0F6C4A45FF4465895FAAC338C6E77 X-Endpoint-Received: by B4 Relay for joel.granados@kernel.org/default with auth_id=239 Add coverage for partially updated sysctl vectors with test 0013. It makes sure that the sysctl vector is unchanged when one of the elements is not valid. Signed-off-by: Joel Granados --- tools/testing/selftests/sysctl/sysctl.sh | 57 ++++++++++++++++++++++++++++= ++++ 1 file changed, 57 insertions(+) diff --git a/tools/testing/selftests/sysctl/sysctl.sh b/tools/testing/selft= ests/sysctl/sysctl.sh index b2d8bd9026a721e96c26be20069f7f87b06a5cf7..fdf3d2fb0a2ad23a525d3205577= a06d7d97576b7 100755 --- a/tools/testing/selftests/sysctl/sysctl.sh +++ b/tools/testing/selftests/sysctl/sysctl.sh @@ -4,6 +4,7 @@ =20 # This performs a series tests against the proc sysctl interface. =20 +# shellcheck disable=3DSC2317 # Kselftest framework requirement - SKIP code is 4. ksft_skip=3D4 =20 @@ -37,6 +38,7 @@ ALL_TESTS=3D"$ALL_TESTS 0009:1:1:unregister_error:0" ALL_TESTS=3D"$ALL_TESTS 0010:1:1:mnt/mnt_error:0" ALL_TESTS=3D"$ALL_TESTS 0011:1:1:empty_add:0" ALL_TESTS=3D"$ALL_TESTS 0012:1:1:u8_valid:0" +ALL_TESTS=3D"$ALL_TESTS 0013:1:1:int_0003:1" =20 function allow_user_defaults() { @@ -196,6 +198,18 @@ verify_diff_w() return $? } =20 +# Verify that an erroneous ($2) update fails and does not change TARGET +verify_no_partial_update() +{ + TEST_STR=3D"$1" + echo -n "$TEST_STR" > "$TARGET" + + if echo -n "$2" > "$TARGET" 2> /dev/null; then + return 1 + fi + verify_diff_w "${TARGET}" +} + test_rc() { if [[ $rc !=3D 0 ]]; then @@ -501,6 +515,40 @@ run_limit_digit_int_array() test_rc } =20 +# You used an int array and one of the elements is not acceptable +run_int_array_no_partial_update() +{ + echo -n "Testing invalid array element does not partially update ... " + # Expect failure because of 3rd element (abc). + if ! verify_no_partial_update "1 2 3 4" "10 20 abc 40"; then + echo "FAIL" >&2 + rc=3D1 + else + echo "OK" + fi + test_rc + + echo -n "Testing out of range array element does not partially update ...= " + # Expect failure because of 3rd element (greater than int) + if ! verify_no_partial_update "1 2 3 4" "10 20 $((INT_MAX + 1)) 40"; then + echo "FAIL" >&2 + rc=3D1 + else + echo "OK" + fi + test_rc + + echo -n "Testing invalid first array element does not update ... " + # Expect failure of 1st element + if ! verify_no_partial_update "1 2 3 4" "abc 20 30 40"; then + echo "FAIL" >&2 + rc=3D1 + else + echo "OK" + fi + test_rc +} + # You are using an unsigned int run_limit_digit_uint() { @@ -880,6 +928,14 @@ sysctl_test_0012() return 0 } =20 +sysctl_test_0013() +{ + TARGET=3D"${SYSCTL}/$(get_test_target 0013)" + reset_vals + + run_int_array_no_partial_update +} + list_tests() { echo "Test ID list:" @@ -900,6 +956,7 @@ list_tests() echo "0010 x $(get_test_count 0010) - tests sysct mount point" echo "0011 x $(get_test_count 0011) - tests empty directories" echo "0012 x $(get_test_count 0012) - tests range check for u8 proc_handl= er" + echo "0013 x $(get_test_count 0013) - tests partially update vectors on e= rror" } =20 usage() --=20 2.50.1