]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
lib: packing: refuse operating on bit indices which exceed size of buffer
authorVladimir Oltean <vladimir.oltean@nxp.com>
Wed, 2 Oct 2024 21:51:50 +0000 (14:51 -0700)
committerJakub Kicinski <kuba@kernel.org>
Thu, 3 Oct 2024 22:32:03 +0000 (15:32 -0700)
While reworking the implementation, it became apparent that this check
does not exist.

There is no functional issue yet, because at call sites, "startbit" and
"endbit" are always hardcoded to correct values, and never come from the
user.

Even with the upcoming support of arbitrary buffer lengths, the
"startbit >= 8 * pbuflen" check will remain correct. This is because
we intend to always interpret the packed buffer in a way that avoids
discontinuities in the available bit indices.

Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Tested-by: Jacob Keller <jacob.e.keller@intel.com>
Signed-off-by: Jacob Keller <jacob.e.keller@intel.com>
Reviewed-by: Przemek Kitszel <przemyslaw.kitszel@intel.com>
Reviewed-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Link: https://patch.msgid.link/20241002-packing-kunit-tests-and-split-pack-unpack-v2-1-8373e551eae3@intel.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
lib/packing.c

index 3f656167c17e0c411e9050c315feecb36f0633d5..439125286d2b4e2cc3ac3313a54793437eedc44e 100644 (file)
@@ -86,8 +86,10 @@ int packing(void *pbuf, u64 *uval, int startbit, int endbit, size_t pbuflen,
         */
        int plogical_first_u8, plogical_last_u8, box;
 
-       /* startbit is expected to be larger than endbit */
-       if (startbit < endbit)
+       /* startbit is expected to be larger than endbit, and both are
+        * expected to be within the logically addressable range of the buffer.
+        */
+       if (unlikely(startbit < endbit || startbit >= 8 * pbuflen || endbit < 0))
                /* Invalid function call */
                return -EINVAL;