drm/dp: Fix OOB read when handling Post Cursor2 register
authorKees Cook <keescook@chromium.org>
Fri, 25 Feb 2022 03:56:10 +0000 (19:56 -0800)
committerThierry Reding <treding@nvidia.com>
Fri, 25 Feb 2022 09:24:20 +0000 (10:24 +0100)
The link_status array was not large enough to read the Adjust Request
Post Cursor2 register, so remove the common helper function to avoid
an OOB read, found with a -Warray-bounds build:

drivers/gpu/drm/drm_dp_helper.c: In function 'drm_dp_get_adjust_request_post_cursor':
drivers/gpu/drm/drm_dp_helper.c:59:27: error: array subscript 10 is outside array bounds of 'const u8[6]' {aka 'const unsigned char[6]'} [-Werror=array-bounds]
   59 |         return link_status[r - DP_LANE0_1_STATUS];
      |                ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
drivers/gpu/drm/drm_dp_helper.c:147:51: note: while referencing 'link_status'
  147 | u8 drm_dp_get_adjust_request_post_cursor(const u8 link_status[DP_LINK_STATUS_SIZE],
      |                                          ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Replace the only user of the helper with an open-coded fetch and decode,
similar to drivers/gpu/drm/amd/display/dc/core/dc_link_dp.c.

Cc: Daniel Vetter <daniel@ffwll.ch>
Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Cc: Maxime Ripard <mripard@kernel.org>
Cc: Thomas Zimmermann <tzimmermann@suse.de>
Cc: David Airlie <airlied@linux.ie>
Cc: dri-devel@lists.freedesktop.org
Fixes: 79465e0ffeb9 ("drm/dp: Add helper to get post-cursor adjustments")
Signed-off-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Reviewed-by: Jani Nikula <jani.nikula@intel.com>
Link: https://lore.kernel.org/r/20220105173507.2420910-1-keescook@chromium.org
Signed-off-by: Thierry Reding <treding@nvidia.com>
drivers/gpu/drm/dp/drm_dp.c
drivers/gpu/drm/tegra/dp.c
include/drm/dp/drm_dp_helper.h

index c43577c8ac4d5f81ca9fd4d6377adc1b1aa8db33..d2fb39f27a764c9437ce98174687358820c6b5e6 100644 (file)
@@ -145,16 +145,6 @@ u8 drm_dp_get_adjust_tx_ffe_preset(const u8 link_status[DP_LINK_STATUS_SIZE],
 }
 EXPORT_SYMBOL(drm_dp_get_adjust_tx_ffe_preset);
 
-u8 drm_dp_get_adjust_request_post_cursor(const u8 link_status[DP_LINK_STATUS_SIZE],
-                                        unsigned int lane)
-{
-       unsigned int offset = DP_ADJUST_REQUEST_POST_CURSOR2;
-       u8 value = dp_link_status(link_status, offset);
-
-       return (value >> (lane << 1)) & 0x3;
-}
-EXPORT_SYMBOL(drm_dp_get_adjust_request_post_cursor);
-
 static int __8b10b_clock_recovery_delay_us(const struct drm_dp_aux *aux, u8 rd_interval)
 {
        if (rd_interval > 4)
index e4369e5b29432677cd4d5cef1c72acc8e73f2e36..7295975e57339b94ae262823b8bef7ae20e10134 100644 (file)
@@ -549,6 +549,15 @@ static void drm_dp_link_get_adjustments(struct drm_dp_link *link,
 {
        struct drm_dp_link_train_set *adjust = &link->train.adjust;
        unsigned int i;
+       u8 post_cursor;
+       int err;
+
+       err = drm_dp_dpcd_read(link->aux, DP_ADJUST_REQUEST_POST_CURSOR2,
+                              &post_cursor, sizeof(post_cursor));
+       if (err < 0) {
+               DRM_ERROR("failed to read post_cursor2: %d\n", err);
+               post_cursor = 0;
+       }
 
        for (i = 0; i < link->lanes; i++) {
                adjust->voltage_swing[i] =
@@ -560,7 +569,7 @@ static void drm_dp_link_get_adjustments(struct drm_dp_link *link,
                                DP_TRAIN_PRE_EMPHASIS_SHIFT;
 
                adjust->post_cursor[i] =
-                       drm_dp_get_adjust_request_post_cursor(status, i);
+                       (post_cursor >> (i << 1)) & 0x3;
        }
 }
 
index 8c277be9626d9463b0b0051850aa6fbdf6c12a49..dad1442c91dffa8300edbe34d4d50c9b7ad378af 100644 (file)
@@ -1525,8 +1525,6 @@ u8 drm_dp_get_adjust_request_pre_emphasis(const u8 link_status[DP_LINK_STATUS_SI
                                          int lane);
 u8 drm_dp_get_adjust_tx_ffe_preset(const u8 link_status[DP_LINK_STATUS_SIZE],
                                   int lane);
-u8 drm_dp_get_adjust_request_post_cursor(const u8 link_status[DP_LINK_STATUS_SIZE],
-                                        unsigned int lane);
 
 #define DP_BRANCH_OUI_HEADER_SIZE      0xc
 #define DP_RECEIVER_CAP_SIZE           0xf