Skip to content

Commit b0b0d81

Browse files
Jason-JH.LinChun-Kuang Hu
authored andcommitted
drm/mediatek: Fix coverity issue with unintentional integer overflow
1. Instead of multiplying 2 variable of different types. Change to assign a value of one variable and then multiply the other variable. 2. Add a int variable for multiplier calculation instead of calculating different types multiplier with dma_addr_t variable directly. Fixes: 1a64a7a ("drm/mediatek: Fix cursor plane no update") Signed-off-by: Jason-JH.Lin <jason-jh.lin@mediatek.com> Reviewed-by: Alexandre Mergnat <amergnat@baylibre.com> Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com> Link: https://patchwork.kernel.org/project/dri-devel/patch/20230907091425.9526-1-jason-jh.lin@mediatek.com/ Signed-off-by: Chun-Kuang Hu <chunkuang.hu@kernel.org>
1 parent 814d534 commit b0b0d81

2 files changed

Lines changed: 38 additions & 10 deletions

File tree

drivers/gpu/drm/mediatek/mtk_drm_gem.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,14 @@ int mtk_drm_gem_dumb_create(struct drm_file *file_priv, struct drm_device *dev,
121121
int ret;
122122

123123
args->pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
124-
args->size = args->pitch * args->height;
124+
125+
/*
126+
* Multiply 2 variables of different types,
127+
* for example: args->size = args->spacing * args->height;
128+
* may cause coverity issue with unintentional overflow.
129+
*/
130+
args->size = args->pitch;
131+
args->size *= args->height;
125132

126133
mtk_gem = mtk_drm_gem_create(dev, args->size, false);
127134
if (IS_ERR(mtk_gem))

drivers/gpu/drm/mediatek/mtk_drm_plane.c

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ static void mtk_plane_update_new_state(struct drm_plane_state *new_state,
141141
dma_addr_t addr;
142142
dma_addr_t hdr_addr = 0;
143143
unsigned int hdr_pitch = 0;
144+
int offset;
144145

145146
gem = fb->obj[0];
146147
mtk_gem = to_mtk_gem_obj(gem);
@@ -150,30 +151,50 @@ static void mtk_plane_update_new_state(struct drm_plane_state *new_state,
150151
modifier = fb->modifier;
151152

152153
if (modifier == DRM_FORMAT_MOD_LINEAR) {
153-
addr += (new_state->src.x1 >> 16) * fb->format->cpp[0];
154-
addr += (new_state->src.y1 >> 16) * pitch;
154+
/*
155+
* Using dma_addr_t variable to calculate with multiplier of different types,
156+
* for example: addr += (new_state->src.x1 >> 16) * fb->format->cpp[0];
157+
* may cause coverity issue with unintentional overflow.
158+
*/
159+
offset = (new_state->src.x1 >> 16) * fb->format->cpp[0];
160+
addr += offset;
161+
offset = (new_state->src.y1 >> 16) * pitch;
162+
addr += offset;
155163
} else {
156164
int width_in_blocks = ALIGN(fb->width, AFBC_DATA_BLOCK_WIDTH)
157165
/ AFBC_DATA_BLOCK_WIDTH;
158166
int height_in_blocks = ALIGN(fb->height, AFBC_DATA_BLOCK_HEIGHT)
159167
/ AFBC_DATA_BLOCK_HEIGHT;
160168
int x_offset_in_blocks = (new_state->src.x1 >> 16) / AFBC_DATA_BLOCK_WIDTH;
161169
int y_offset_in_blocks = (new_state->src.y1 >> 16) / AFBC_DATA_BLOCK_HEIGHT;
162-
int hdr_size;
170+
int hdr_size, hdr_offset;
163171

164172
hdr_pitch = width_in_blocks * AFBC_HEADER_BLOCK_SIZE;
165173
pitch = width_in_blocks * AFBC_DATA_BLOCK_WIDTH *
166174
AFBC_DATA_BLOCK_HEIGHT * fb->format->cpp[0];
167175

168176
hdr_size = ALIGN(hdr_pitch * height_in_blocks, AFBC_HEADER_ALIGNMENT);
177+
hdr_offset = hdr_pitch * y_offset_in_blocks +
178+
AFBC_HEADER_BLOCK_SIZE * x_offset_in_blocks;
179+
180+
/*
181+
* Using dma_addr_t variable to calculate with multiplier of different types,
182+
* for example: addr += hdr_pitch * y_offset_in_blocks;
183+
* may cause coverity issue with unintentional overflow.
184+
*/
185+
hdr_addr = addr + hdr_offset;
169186

170-
hdr_addr = addr + hdr_pitch * y_offset_in_blocks +
171-
AFBC_HEADER_BLOCK_SIZE * x_offset_in_blocks;
172187
/* The data plane is offset by 1 additional block. */
173-
addr = addr + hdr_size +
174-
pitch * y_offset_in_blocks +
175-
AFBC_DATA_BLOCK_WIDTH * AFBC_DATA_BLOCK_HEIGHT *
176-
fb->format->cpp[0] * (x_offset_in_blocks + 1);
188+
offset = pitch * y_offset_in_blocks +
189+
AFBC_DATA_BLOCK_WIDTH * AFBC_DATA_BLOCK_HEIGHT *
190+
fb->format->cpp[0] * (x_offset_in_blocks + 1);
191+
192+
/*
193+
* Using dma_addr_t variable to calculate with multiplier of different types,
194+
* for example: addr += pitch * y_offset_in_blocks;
195+
* may cause coverity issue with unintentional overflow.
196+
*/
197+
addr = addr + hdr_size + offset;
177198
}
178199

179200
mtk_plane_state->pending.enable = true;

0 commit comments

Comments
 (0)