Commit Graph

6 Commits

Author SHA1 Message Date
Petr Oros df788e1a81 overflow: Change DEFINE_FLEX to take __counted_by member
JIRA: https://issues.redhat.com/browse/RHEL-49816

Conflicts:
- adjusted context conflict due to missing 4b21d25bf519c9 ("overflow:
  Introduce overflows_type() and castable_to_type()")
- Used DEFINE_RAW_FLEX() in ice_switch.c. DEFINE_FLEX() usage was
  introduced as conflict resolution in commit db06e13e87 ("ice:
  Add switch recipe reusing feature")

Upstream commit(s):
commit d8e45f2929b94099913eb66c3ebb18b5063e9421
Author: Kees Cook <keescook@chromium.org>
Date:   Wed Mar 6 15:51:36 2024 -0800

    overflow: Change DEFINE_FLEX to take __counted_by member

    The norm should be flexible array structures with __counted_by
    annotations, so DEFINE_FLEX() is updated to expect that. Rename
    the non-annotated version to DEFINE_RAW_FLEX(), and update the
    few existing users. Additionally add selftests for the macros.

    Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>
    Link: https://lore.kernel.org/r/20240306235128.it.933-kees@kernel.org
    Reviewed-by: Przemek Kitszel <przemyslaw.kitszel@intel.com>
    Signed-off-by: Kees Cook <keescook@chromium.org>

Signed-off-by: Petr Oros <poros@redhat.com>
2024-12-09 13:12:32 +01:00
Ivan Vecera 450fc1e4ce overflow: Allow mixed type arguments
JIRA: https://issues.redhat.com/browse/RHEL-36218

commit d219d2a9a92e39aa92799efe8f2aa21259b6dd82
Author: Kees Cook <keescook@chromium.org>
Date:   Mon Aug 29 13:37:17 2022 -0700

    overflow: Allow mixed type arguments

    When the check_[op]_overflow() helpers were introduced, all arguments
    were required to be the same type to make the fallback macros simpler.
    However, now that the fallback macros have been removed[1], it is fine
    to allow mixed types, which makes using the helpers much more useful,
    as they can be used to test for type-based overflows (e.g. adding two
    large ints but storing into a u8), as would be handy in the drm core[2].

    Remove the restriction, and add additional self-tests that exercise
    some of the mixed-type overflow cases, and double-check for accidental
    macro side-effects.

    [1] https://git.kernel.org/linus/4eb6bd55cfb22ffc20652732340c4962f3ac9a91
    [2] https://lore.kernel.org/lkml/20220824084514.2261614-2-gwan-gyeong.mun@intel.com

    Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
    Cc: Gwan-gyeong Mun <gwan-gyeong.mun@intel.com>
    Cc: "Gustavo A. R. Silva" <gustavoars@kernel.org>
    Cc: Nick Desaulniers <ndesaulniers@google.com>
    Cc: linux-hardening@vger.kernel.org
    Reviewed-by: Andrzej Hajda <andrzej.hajda@intel.com>
    Reviewed-by: Gwan-gyeong Mun <gwan-gyeong.mun@intel.com>
    Tested-by: Gwan-gyeong Mun <gwan-gyeong.mun@intel.com>
    Signed-off-by: Kees Cook <keescook@chromium.org>

Signed-off-by: Ivan Vecera <ivecera@redhat.com>
2024-05-14 11:12:51 +02:00
Petr Oros 27e4be54a5 overflow: Add struct_size_t() helper
JIRA: https://issues.redhat.com/browse/RHEL-30138

Conflicts:
- removed chunk for drivers/scsi/hptiop.c due to missing c682df71996aee
  ("scsi: hptiop: Replace one-element array with flexible-array member")
  and 5b12a568cc6f69 ("scsi: hptiop: Use struct_size() helper in code
  related to struct hpt_iop_request_scsi_command"). The driver is disabled
  by config.

Upstream commit(s):
commit d67790ddf0219aa0ad3e13b53ae0a7619b3425a2
Author: Kees Cook <keescook@chromium.org>
Date:   Mon May 22 14:18:13 2023 -0700

    overflow: Add struct_size_t() helper

    While struct_size() is normally used in situations where the structure
    type already has a pointer instance, there are places where no variable
    is available. In the past, this has been worked around by using a typed
    NULL first argument, but this is a bit ugly. Add a helper to do this,
    and replace the handful of instances of the code pattern with it.

    Instances were found with this Coccinelle script:

    @struct_size_t@
    identifier STRUCT, MEMBER;
    expression COUNT;
    @@

    -       struct_size((struct STRUCT *)\(0\|NULL\),
    +       struct_size_t(struct STRUCT,
                    MEMBER, COUNT)

    Suggested-by: Christoph Hellwig <hch@infradead.org>
    Cc: Jesse Brandeburg <jesse.brandeburg@intel.com>
    Cc: Tony Nguyen <anthony.l.nguyen@intel.com>
    Cc: "David S. Miller" <davem@davemloft.net>
    Cc: Eric Dumazet <edumazet@google.com>
    Cc: Paolo Abeni <pabeni@redhat.com>
    Cc: James Smart <james.smart@broadcom.com>
    Cc: Keith Busch <kbusch@kernel.org>
    Cc: Jens Axboe <axboe@kernel.dk>
    Cc: Sagi Grimberg <sagi@grimberg.me>
    Cc: HighPoint Linux Team <linux@highpoint-tech.com>
    Cc: "James E.J. Bottomley" <jejb@linux.ibm.com>
    Cc: "Martin K. Petersen" <martin.petersen@oracle.com>
    Cc: Kashyap Desai <kashyap.desai@broadcom.com>
    Cc: Sumit Saxena <sumit.saxena@broadcom.com>
    Cc: Shivasharan S <shivasharan.srikanteshwara@broadcom.com>
    Cc: Don Brace <don.brace@microchip.com>
    Cc: "Darrick J. Wong" <djwong@kernel.org>
    Cc: Dave Chinner <dchinner@redhat.com>
    Cc: Guo Xuenan <guoxuenan@huawei.com>
    Cc: Gwan-gyeong Mun <gwan-gyeong.mun@intel.com>
    Cc: Nick Desaulniers <ndesaulniers@google.com>
    Cc: Daniel Latypov <dlatypov@google.com>
    Cc: kernel test robot <lkp@intel.com>
    Cc: intel-wired-lan@lists.osuosl.org
    Cc: netdev@vger.kernel.org
    Cc: linux-nvme@lists.infradead.org
    Cc: linux-scsi@vger.kernel.org
    Cc: megaraidlinux.pdl@broadcom.com
    Cc: storagedev@microchip.com
    Cc: linux-xfs@vger.kernel.org
    Cc: linux-hardening@vger.kernel.org
    Signed-off-by: Kees Cook <keescook@chromium.org>
    Acked-by: Martin K. Petersen <martin.petersen@oracle.com>
    Reviewed-by: Darrick J. Wong <djwong@kernel.org>
    Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>
    Reviewed-by: Christoph Hellwig <hch@lst.de>
    Acked-by: Jakub Kicinski <kuba@kernel.org>
    Reviewed-by: Alexander Lobakin <aleksander.lobakin@intel.com>
    Link: https://lore.kernel.org/r/20230522211810.never.421-kees@kernel.org

Signed-off-by: Petr Oros <poros@redhat.com>
2024-03-26 21:44:33 +01:00
Nico Pache 3d23c44f89 lib: overflow: update reference to kunit-tool
commit 0f3f1123ac6e14a2c0a32f7a43163e6704beb74f
Author: Tales Aparecida <tales.aparecida@gmail.com>
Date:   Sun Aug 21 23:26:45 2022 -0300

    lib: overflow: update reference to kunit-tool

    Replace URL with an updated path to the full Documentation page

    Signed-off-by: Tales Aparecida <tales.aparecida@gmail.com>
    Reviewed-by: Kees Cook <keescook@chromium.org>
    Reviewed-by: David Gow <davidgow@google.com>
    Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

JIRA: https://issues.redhat.com/browse/RHEL-5618
Signed-off-by: Nico Pache <npache@redhat.com>
2023-11-02 15:26:53 -06:00
Nico Pache 8b2bf8dbbb lib: overflow: Do not define 64-bit tests on 32-bit
commit 6a022dd29f2cefbac4895a34e2e1f14b2d12d819
Author: Kees Cook <keescook@chromium.org>
Date:   Wed May 11 10:40:43 2022 -0700

    lib: overflow: Do not define 64-bit tests on 32-bit

    The 64-bit overflow tests will trigger 64-bit division on 32-bit hosts,
    which is not currently used anywhere in the kernel, and tickles bugs
    in at least Clang 13 and earlier:
    https://github.com/ClangBuiltLinux/linux/issues/1636

    In reality, there shouldn't be a reason to not build the 64-bit test
    cases on 32-bit systems, so these #ifdefs can be removed once the minimum
    Clang version reaches 13.

    In the meantime, silence W=1 warnings given by the current code:

    ../lib/overflow_kunit.c:191:19: warning: 's64_tests' defined but not used [-Wunused-const-variable=]
      191 | DEFINE_TEST_ARRAY(s64) = {
          |                   ^~~
    ../lib/overflow_kunit.c:24:11: note: in definition of macro 'DEFINE_TEST_ARRAY'
       24 |         } t ## _tests[]
          |           ^
    ../lib/overflow_kunit.c:94:19: warning: 'u64_tests' defined but not used [-Wunused-const-variable=]
       94 | DEFINE_TEST_ARRAY(u64) = {
          |                   ^~~
    ../lib/overflow_kunit.c:24:11: note: in definition of macro 'DEFINE_TEST_ARRAY'
       24 |         } t ## _tests[]
          |           ^

    Reported-by: kernel test robot <lkp@intel.com>
    Link: https://lore.kernel.org/lkml/202205110324.7GrtxG8u-lkp@intel.com
    Fixes: 455a35a6cd ("lib: add runtime test of check_*_overflow functions")
    Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
    Cc: Nick Desaulniers <ndesaulniers@google.com>
    Cc: Vitor Massaru Iha <vitor@massaru.org>
    Cc: "Gustavo A. R. Silva" <gustavoars@kernel.org>
    Tested-by: Daniel Latypov <dlatypov@google.com>
    Link: https://lore.kernel.org/lkml/CAGS_qxokQAjQRip2vPi80toW7hmBnXf=KMTNT51B1wuDqSZuVQ@mail.gmail.com
    Signed-off-by: Kees Cook <keescook@chromium.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2168378
Signed-off-by: Nico Pache <npache@redhat.com>
2023-04-17 11:47:36 -06:00
Nico Pache 0ad6ac8b08 lib: overflow: Convert to Kunit
commit 617f55e20743fc50c989b498f9dee289eb644cfd
Author: Kees Cook <keescook@chromium.org>
Date:   Wed Feb 16 14:17:49 2022 -0800

    lib: overflow: Convert to Kunit

    Convert overflow unit tests to KUnit, for better integration into the
    kernel self test framework. Includes a rename of test_overflow.c to
    overflow_kunit.c, and CONFIG_TEST_OVERFLOW to CONFIG_OVERFLOW_KUNIT_TEST.

    $ ./tools/testing/kunit/kunit.py run overflow
    ...
    [14:33:51] Starting KUnit Kernel (1/1)...
    [14:33:51] ============================================================
    [14:33:51] ================== overflow (11 subtests) ==================
    [14:33:51] [PASSED] u8_overflow_test
    [14:33:51] [PASSED] s8_overflow_test
    [14:33:51] [PASSED] u16_overflow_test
    [14:33:51] [PASSED] s16_overflow_test
    [14:33:51] [PASSED] u32_overflow_test
    [14:33:51] [PASSED] s32_overflow_test
    [14:33:51] [PASSED] u64_overflow_test
    [14:33:51] [PASSED] s64_overflow_test
    [14:33:51] [PASSED] overflow_shift_test
    [14:33:51] [PASSED] overflow_allocation_test
    [14:33:51] [PASSED] overflow_size_helpers_test
    [14:33:51] ==================== [PASSED] overflow =====================
    [14:33:51] ============================================================
    [14:33:51] Testing complete. Passed: 11, Failed: 0, Crashed: 0, Skipped: 0, Errors: 0
    [14:33:51] Elapsed time: 12.525s total, 0.001s configuring, 12.402s building, 0.101s running

    Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
    Cc: Nick Desaulniers <ndesaulniers@google.com>
    Co-developed-by: Vitor Massaru Iha <vitor@massaru.org>
    Signed-off-by: Vitor Massaru Iha <vitor@massaru.org>
    Link: https://lore.kernel.org/lkml/20200720224418.200495-1-vitor@massaru.org/
    Co-developed-by: Daniel Latypov <dlatypov@google.com>
    Signed-off-by: Daniel Latypov <dlatypov@google.com>
    Link: https://lore.kernel.org/linux-kselftest/20210503211536.1384578-1-dlatypov@google.com/
    Acked-by: Nick Desaulniers <ndesaulniers@google.com>
    Link: https://lore.kernel.org/lkml/CAKwvOdm62iA1dNiC6Q11UJ-MnTqtc4kXkm-ubPaFMK824_k0nw@mail.gmail.com
    Signed-off-by: Kees Cook <keescook@chromium.org>
    Reviewed-by: David Gow <davidgow@google.com>
    Link: https://lore.kernel.org/lkml/CABVgOS=TWVh649_Vjo3wnMu9gZnq66gkV-LtGgsksAWMqc+MSA@mail.gmail.com

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2168378
Signed-off-by: Nico Pache <npache@redhat.com>
2023-04-17 11:47:31 -06:00