From nobody Tue Sep 29 00:31:23 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 A4E133D88E3; Fri, 14 Aug 2026 10:41:25 +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=1786704085; cv=none; b=rnMR5Nu7bw7cXiEojV82Kz0kK/zVNf18UdWFOLQ1AYpEx4MtISlaz3VwtYzwokch6od5+MKzvL417q2yIxIHq0X1aqp55iCsO41fWLNVNf62+9kMI1HJXEGLZvH7088/0b7Ms+6OHYrxqz2UcEpHhhd2fYEtLYo0k6T1WxfJDLU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786704085; c=relaxed/simple; bh=wc51Ar1jilhnjUGqKXjw2tY4+e0678L6LjQxN5ZQWOY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=bi0QL3Sbsmznc80gIc4Wi/qzqMWIcX2MQy50dVuG/ZFbetd2yRHvEpbMyBuEjPRsWxO99aM+VtGksJ0V8khOUMRnrx8K+/vvvnf+M+EBo4ooMn0Hb+LCSNR5FT3FGt3/zjXKltgV9uFkSL82OrR+yi5LE9abJ/rKkVbQLPFPn3M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=dQHUmCJQ; 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="dQHUmCJQ" Received: by smtp.kernel.org (Postfix) with ESMTPS id A9444C2BCB9; Fri, 14 Aug 2026 10:41:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1786704083; bh=wc51Ar1jilhnjUGqKXjw2tY4+e0678L6LjQxN5ZQWOY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=dQHUmCJQbHpjKnszykCVG2CXecJtAI1vFAeTZz+5JJU2z9FGb+3OXBQ/bCdNSHiL5 2BzxxnDlk8zvKNQKQTN/ZuzEFCzHtZbpqT1NJFU3iQLEIq70SotgLWtuIDjRitnMuE N0dRUrKmmswfRgW1DAlN8g/CkvxXImDojG4hVVrTFOVi2RPvvDWfTe6l988H44BkeE ExNQVoZ08YfZ1nNrgFW1gdMiwIYytZNqvTu5heMOc1TnLEkaVl4txKhJF2AQvqtyNH 1K4iar9cDdS8YFn9FLl83jw9nbk5m3o8r2xdUoqY6Ldvgri/oaF/QB4J6GpzO9aHlg PLByPtR7Qzhdg== 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 8792DC5B572; Fri, 14 Aug 2026 10:41:23 +0000 (UTC) From: Joel Granados Date: Fri, 14 Aug 2026 12:41:12 +0200 Subject: [PATCH v2 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: <20260814-lklm-partial_ctlvec-v2-1-9df50d26e477@kernel.org> References: <20260814-lklm-partial_ctlvec-v2-0-9df50d26e477@kernel.org> In-Reply-To: <20260814-lklm-partial_ctlvec-v2-0-9df50d26e477@kernel.org> To: Kees Cook , Shuah Khan 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, linux-mm@kvack.org, Joel Granados X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=12066; i=joel.granados@kernel.org; h=from:subject:message-id; bh=wc51Ar1jilhnjUGqKXjw2tY4+e0678L6LjQxN5ZQWOY=; b=owJ4nAHtARL+kA0DAAoBupfNUreWQU8ByyZiAGp+8M8vr+lEf7V6BRsfLebGaWNXeMT1k+ohr RnFxBUiAJih0YkBswQAAQoAHRYhBK5HCVcl5jElzssnkLqXzVK3lkFPBQJqfvDPAAoJELqXzVK3 lkFPMfEL/0TFDnPZxzlhtXUe9sCMJNET1aPiqy5DcZpclEhrldW8L1wXDZr8CQcQcCghr2pqD1R 0lnclMJMoJL+CnnpGkKKnpGIfNJlaEIsGxWP9gZtKY0H++d5VKmcZBiVJW+Eh3eOQSBslAwiZOQ Pie6ryhgU9ey1vQfJcuZ7fPgjrkU6IrBEMgSlKL3JuToGg2ZKx6m9q50HvR6CNAS/WfyTQi13G1 APpUpKdLdYZD8A6WgS/MbMJioHFOvyHNHW2aKOvb/Q6C927bPNb/n9kM30BOE6//f1Dj+bPn9Ia EAdTLDB6Zcgc3DfEeASQ3U19FmWqGUFI9+SKbyv6PBi8TEzXq0PtFaBTH9F4LYFt86LGlsAqT8o tim5ER91NcIV8h9Z6ERaa+AQgKyI/oexSGdGA1JxcVLGpSau4y2l1v+gqPLjSok21K/9VBRKZWZ vROgkt1ttZ11Ku4CXJiQ58HYK2Rvz3Bm0nfODk+OJ9mGkrZJaYQ7e/7gsZRTPBWMbqGQWaLsuL4 OU= 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 Reviewed-by: Bradley Morgan Tested-by: Bradley Morgan for the test --- kernel/sysctl.c | 211 +++++++++++++++++++++++++++++++++-------------------= ---- 1 file changed, 126 insertions(+), 85 deletions(-) diff --git a/kernel/sysctl.c b/kernel/sysctl.c index f7b75985d5420a522a316d1fade40dc7fbe60455..47a92cbbcb69cd361a18dba6606= ae6ae8f86b5e2 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,142 @@ 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 + * + * @conv: The converter to be applied + * @table: The sysctl table + * @data_type: Type used in converter appliation (INT, UINT or ULONG) + * @data_size: Number of data elements. + * @conv_dir: %TRUE if this is a write to the sysctl file + * @buf_nbyte: Number of bytes for buf + * @buf: The user buffer + * @buf_left_final: Number of outstanding (non converted) bytes. + * + * 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 +801,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 +861,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 +881,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 +905,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 +932,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 +976,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 +1107,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 +1128,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 +1155,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 00:31:23 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 A94B04570C3; Fri, 14 Aug 2026 10:41:25 +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=1786704085; cv=none; b=UnzZvavCz2dONsrfcG8N45iYvBi4z1g6gwFvbAlKPBdu9Js4/05WKjZmhUuzaoaMe711U5A7hm2UnpoyCWgG59DsC5kz7s9rYi8I6MTKc1czJNdAdfSSsOTMzNZwY2p3cS735+Md9J7WslBxJEItss57uYP4L2insBSHcK1l6Ao= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786704085; c=relaxed/simple; bh=ACmHDzg0bdqhRKtZiggZe9yyDKsjLpeE7LXC/BUIylE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Gh5nZoSXE39M1S+2I+tsFTXdPdIcp8qpyiXAY++jq4Dzwa8+/2yfvQYFvqAXlqqoT5PGRGzRp29P9XnSeBM8vmRfUUjb5JUtP8cQl04k8hZwJsvd3+GBruQwsHbBUyQUivXy+Z9ukLAqWkxn9EEGl2Wov0bVav0TwJOOBFHiHVQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=a36d0zwY; 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="a36d0zwY" Received: by smtp.kernel.org (Postfix) with ESMTPS id B705CC4AF09; Fri, 14 Aug 2026 10:41:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1786704083; bh=ACmHDzg0bdqhRKtZiggZe9yyDKsjLpeE7LXC/BUIylE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=a36d0zwY52gz/cdEaRmS3cDOVCWTSZxFJ9ECsn9afWIL7bpOeKRzCS2m0MayPvOO8 VbiHHEJbLBMyxASNIUNwFlXpv25dFmldauNkqmyqnai/mMGMA6PGrdmcFJjOf2ANza USfRD9/8INS7JurPTEVmyNwa3IBudfK3BVqaC8G4qFyKYh3x3Iaj8nKKa6/7sCxgQw Lu7aYoIdEizHAphuamXXC2qIwmgRaT9UMZ8pZeRIFx7DeE/gb7FufC3GK1qUJH0Aje urh6gyVKOm6lItITCY1xHOUPGp+zsCvNEq0SIyUceSnWHZKtZwcZe5KxBZI+m1CM8m SVmZijnojIRMw== 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 96FFDC5CFC1; Fri, 14 Aug 2026 10:41:23 +0000 (UTC) From: Joel Granados Date: Fri, 14 Aug 2026 12:41:13 +0200 Subject: [PATCH v2 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: <20260814-lklm-partial_ctlvec-v2-2-9df50d26e477@kernel.org> References: <20260814-lklm-partial_ctlvec-v2-0-9df50d26e477@kernel.org> In-Reply-To: <20260814-lklm-partial_ctlvec-v2-0-9df50d26e477@kernel.org> To: Kees Cook , Shuah Khan 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, linux-mm@kvack.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=ACmHDzg0bdqhRKtZiggZe9yyDKsjLpeE7LXC/BUIylE=; b=owJ4nAHtARL+kA0DAAoBupfNUreWQU8ByyZiAGp+8NDu6mELinrd4Zbsp71ht/npxGd5//lUz p/ms+oZ3YkI2okBswQAAQoAHRYhBK5HCVcl5jElzssnkLqXzVK3lkFPBQJqfvDQAAoJELqXzVK3 lkFPOlcL/A582yimDhlcHkvz9SG0STpA5Qs3yhtyNi7XgDJdTE3l5T8et6PDFryntBnyzN/h2Kb NQ8Bm1TNmZLmNrZj/NQxdhp9obZAgFaePZAb6h6SKmZjZls60bDttBqmoufK+lz/qhD2tFpDYgV yM5RWJxWi3BZLDP9Fkxhw8ApM4bs+B9BC2DshT1jveym+shgHuts1D+sBhnH4RdWucKgvFl6Q7u mrbpywg0cl1GHRhe6ZwasXkLgrwUJs5oRw0cVCop4SqzLWipln452fskR6/WpF1p8YsmPT2s9ZN 0jIS9/MI/aMXGFbfTo7wYZ5V7J6jj6Rg58gxXnKf2g7S/Q+Fi6deXQy0/izZ46RO2Qc9PC/bUWT KWz8Ysec+jRY7bSr8F5GZnSRVO5bZuOJW7YJtIMLiKU2rGq2ZTaK6TnMg17q0N3Kc//OJzGADvf pgMhe6EVetnBHd2ffgh9b3c6rM4wkI3X1GJAmzv1BsNxbsPWo5YTXWC6NSogE++M/IHTarsIUi9 ak= 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 Reviewed-by: Bradley Morgan Tested-by: Bradley Morgan for the test --- kernel/sysctl.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/kernel/sysctl.c b/kernel/sysctl.c index 47a92cbbcb69cd361a18dba6606ae6ae8f86b5e2..7e9024899be6d5971752dd5639a= b4b806a899081 100644 --- a/kernel/sysctl.c +++ b/kernel/sysctl.c @@ -748,10 +748,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; @@ -797,6 +793,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; @@ -881,8 +880,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 /** @@ -932,8 +930,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 /** @@ -976,8 +974,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 00:31:23 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 A5E08456E14; Fri, 14 Aug 2026 10:41:25 +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=1786704085; cv=none; b=JQsVtmem74zcXWdeDPJsI2tX/yzRH/47hRjXDr67xu7FPgdoLFaZfy/awIAjrE8VcINUJ6XrcB5lapIkgQb1oBl4VM+ARbI9LEvDQiEX+viJI1ZII9R9wRNq5LCqr/f6MUhfn1Xf46JJ7Qlic5syIbngx9YfWR0x4KZccSa4ItU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786704085; c=relaxed/simple; bh=CJlMe8eckMtRbFMo6OjTUfLXAWdcwp7d+38BdYAigIE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=j3FRUWEppm+3CTbWHUHTcYpA20CSOeEVo//v9cciQRVnODv4OT2mGBnu+1f6wVbbHeuxKaxOO3q8xtYjB2Ku5JR7m1ccAFXeNrteNnnJNB0Ag8ncSoLU4pk+V7Zh6b/ZSC2N+Z1rT8NIoSXQZXKrXRBcajVpVkpz5LB25WQzneU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=eKK7b8HQ; 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="eKK7b8HQ" Received: by smtp.kernel.org (Postfix) with ESMTPS id BC9EBC2BCF7; Fri, 14 Aug 2026 10:41:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1786704083; bh=CJlMe8eckMtRbFMo6OjTUfLXAWdcwp7d+38BdYAigIE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=eKK7b8HQktyx7HHxZzmr9+dwYq8G83AG+MtacqM6wPAyyvVlvZ50/BTv1ASYdCBgc M4pdva1YgaBnVbRkuI+nBkfipPy9wJKzTaS7OmWzd1+FQTpWqbOjoZQW/rnyVZEe0J bnjZC8VHcm9IAJbtxBfLzT4wtEcf449pr5CmNo1KpZKkREQUcSg9r91atpIBL6lRDc rwb5zFOcLEsgNJFLT5yGynBMvYMwc3kFo5RIFPsiIiJ3PSVC3TpUo2TbdxH3tIHZfM Yl59Ff1n6D/oFNcosGzKX7dV50UqzPAC6zfHN+L+P4knGnnxWGgJJL2CePLyjPbVbR fyZqVEIkog3Zg== 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 A7F22C5DF6A; Fri, 14 Aug 2026 10:41:23 +0000 (UTC) From: Joel Granados Date: Fri, 14 Aug 2026 12:41:14 +0200 Subject: [PATCH v2 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: <20260814-lklm-partial_ctlvec-v2-3-9df50d26e477@kernel.org> References: <20260814-lklm-partial_ctlvec-v2-0-9df50d26e477@kernel.org> In-Reply-To: <20260814-lklm-partial_ctlvec-v2-0-9df50d26e477@kernel.org> To: Kees Cook , Shuah Khan 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, linux-mm@kvack.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=CJlMe8eckMtRbFMo6OjTUfLXAWdcwp7d+38BdYAigIE=; b=owJ4nAHtARL+kA0DAAoBupfNUreWQU8ByyZiAGp+8NHUjrC7/UY5qTi0oMM3LIpSbQYbXCUtz uIVIOsjNamHHIkBswQAAQoAHRYhBK5HCVcl5jElzssnkLqXzVK3lkFPBQJqfvDRAAoJELqXzVK3 lkFPMgUL/1+0vc9a7WaBT4uQrdf6jczPCFm0E/iBcZQPHgbAfV+CEAmg0uJf2GHtzgs9xmRKM6E HxBTtxcUx5A/LhJWwVQberzSHPAf1BaKBa5ymaidcfZNBCG0TEn5hVChTZqPHT8m5frNEYt4JQu Ao5xgFF2wgHM4ExTr5rB1xccWG8AF+FRZI+YcR747VuBx2BUd70cOmQeTrxSyoITjkOVzL9Ad6Y 0owmDRcqrx3GJRa8xcK0CCuw9nLGB53pQfkAJhBTkD+B9OKgf6CmgHLLJsUqreQzPsRJsxR7f2k PKXfYS7UZTBKMrKouFiXKAjAiX49LXafoFkwGzwrsFIfBeliqQPkw+YFHmxfbJnHvCawt37SPdM x1qVyHmVRqzyp73hHuxn3IGLUBsjURiyVRGNdAHyRMnwm+pnL1NoZI/vNGRUg3qVXKOo5C4Xcn7 EOPhbwbV3bTl7u+SWBFdqsthVkyw0VGeOE2sGm6a/4+oG/3YeolYydFWmyZnrtKnghQE32o11K4 2M= 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 Reviewed-by: Bradley Morgan Tested-by: Bradley Morgan for the test --- kernel/sysctl.c | 65 +++++++++++++++++++++++++++++++++++++++++++++--------= ---- 1 file changed, 52 insertions(+), 13 deletions(-) diff --git a/kernel/sysctl.c b/kernel/sysctl.c index 7e9024899be6d5971752dd5639ab4b806a899081..25577ffc2801267cc69df60a106= 4ada9efd4b97d 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 * @@ -663,22 +683,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 @@ -703,20 +732,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 00:31:23 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 A5F1E4570C2; Fri, 14 Aug 2026 10:41:25 +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=1786704085; cv=none; b=kAlve37aen2jcjgZzKieJQHqg++SCZkNPopIqeGPwYPmlEm1PlQqmbeMx7J2x/kLkxptEVfTE3eIdJSd28/wZvcMx2akiEEPjDbhEQQNti0xzMdT7FcRNChQXEbhNVje2LFbMEVxuJ9JGtjCHT4xN95KL5NuVD+rHeOxULgLgCM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786704085; c=relaxed/simple; bh=YHI3VBORAsd9SqfY5aar0OvGzZqdtGpSqI6smTfSLXY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=d5x2xdu4mI3pcnUzM6Cn9G5owQ/OqBsrn74G50DM5ssvOhHhIAFwkU2Wk5marLmisgp8kZFu6/riptcZNj6jqgkxaHfAOi1amlhR/jYz0ks7aZJZwUh5zyG7GgtaR/yNvcXGgXApj/A86kNwCeYNima/VPPftpRM906bu5LrJmw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gvZR6r2s; 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="gvZR6r2s" Received: by smtp.kernel.org (Postfix) with ESMTPS id CEE67C2BD00; Fri, 14 Aug 2026 10:41:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1786704083; bh=YHI3VBORAsd9SqfY5aar0OvGzZqdtGpSqI6smTfSLXY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=gvZR6r2sAk+83hGeBZm5n2MK5vP+Q1JS+FYof1FPUtkEr8K1hckh+rQfCr+iMnC0/ 7OGsT0A727GWC48yIcVuyfkDCvsjvwhaUPykcg4Qozpy6ktL/k4i2ddt6T3ldiCSw9 xPc3tY2fdCn9UqUOuXAPIOnJ5ZZD5XsWsEdTFG+7bk027mB1uu+/HqmR+YdzbfLfp/ K+jmx6X/yCeCtwwoAWXVcD4oqC/4ey9tCvYTm6cXAM3TBAqo8suNZpTuTPJ5vq/UfU aQ2wh3gix4NxPEg1Kchd/gu2uhXS9hoO4vCNuzZnvHNiR24secduk/o9oNxB5xWfv4 FFxvEb4AgApEg== 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 B5602C5DF67; Fri, 14 Aug 2026 10:41:23 +0000 (UTC) From: Joel Granados Date: Fri, 14 Aug 2026 12:41:15 +0200 Subject: [PATCH v2 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: <20260814-lklm-partial_ctlvec-v2-4-9df50d26e477@kernel.org> References: <20260814-lklm-partial_ctlvec-v2-0-9df50d26e477@kernel.org> In-Reply-To: <20260814-lklm-partial_ctlvec-v2-0-9df50d26e477@kernel.org> To: Kees Cook , Shuah Khan 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, linux-mm@kvack.org, Joel Granados X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=3356; i=joel.granados@kernel.org; h=from:subject:message-id; bh=YHI3VBORAsd9SqfY5aar0OvGzZqdtGpSqI6smTfSLXY=; b=owJ4nAHtARL+kA0DAAoBupfNUreWQU8ByyZiAGp+8NEZx7DhoWX+VVmoWYcszozrHrTSVyWWi NensKkTznzrsIkBswQAAQoAHRYhBK5HCVcl5jElzssnkLqXzVK3lkFPBQJqfvDRAAoJELqXzVK3 lkFP5nkL+gOLrefeg7tdLifVFAOWe9dtn+spU/1WLrN/o+x2sYe9+IVhJQKAT8S1ikOZr0D1eNa ASVm4Sz9LPGPHEAUuF9EtwF6qqrWg/3ZNGYZ+X05idINQ3jROzIMrcxRglhxOCE+8MYZ5lwAume VeSpQStAWQcO4TqMHUs9CiJrvxHSS4AEnddKvwFH+AhN+5nahkgwpXHAvtvN4CXFpYpZlCeUE/Y gCRVJQ3ikhBtkDn5YOhXbPoiKRtqTnPiSEfG87ibN8hILuti4JAeuNrY9pzJuH4D3Clicp3hxsg LV3YL3CXAYi3oNKW9vq4SI+YGEoSOQUMEwfAI9fEDSI9XNEgNc3ENL7IfDDiWayX9crn7X/HD8C ILHDSwaMPOP/+pwf8GUE73GS6ow2R/Z0geldHXgpe/IHZopgMiwlcL3VzouyJm67V1bq7dSwLFf +d6DLLGQUDuYHBQk8Ds/ZZjVtQjNL4h8nRc26WCMiblkgBQzLFSAwZHjwBkT7ysaMoM8kGxwi95 08= 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 Reviewed-by: Bradley Morgan Tested-by: Bradley Morgan for the test --- tools/testing/selftests/sysctl/sysctl.sh | 60 ++++++++++++++++++++++++++++= ++++ 1 file changed, 60 insertions(+) diff --git a/tools/testing/selftests/sysctl/sysctl.sh b/tools/testing/selft= ests/sysctl/sysctl.sh index b2d8bd9026a721e96c26be20069f7f87b06a5cf7..dc8674455284612dfe898944b88= 6d856fb8ab3fe 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 @@ -12,6 +13,9 @@ TEST_DRIVER=3D"test_${TEST_NAME}" TEST_DIR=3D$(dirname $0) TEST_FILE=3D$(mktemp) =20 +# initialize for individual tests +rc=3D0 + # This represents # # TEST_ID:TEST_COUNT:ENABLED:TARGET:SKIP_NO_TARGET @@ -37,6 +41,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 +201,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 +518,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 +931,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 +959,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