misc: fastrpc: reject overflowing invoke payload sizes
PATCH 1/1 misc: fastrpc: reject overflowing invoke payload sizes
fastrpc_get_payload_size() accumulates the metadata and inline argument payload sizes before allocating the coherent invoke buffer. User-provided argument ranges can make this accumulator wrap, leaving a buffer smaller than the later serialization expects.
Return an error when the payload size arithmetic overflows or cannot be represented as an allocation size. This prevents an undersized DMA buffer from being used for invoke argument serialization.
--- drivers/misc/fastrpc.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)
drivers/misc/fastrpc.c +14 -4
@@ -997,7 +997,8 @@ static int fastrpc_get_meta_size(struct fastrpc_invoke_ctx *ctx)return size; } -static u64 fastrpc_get_payload_size (struct fastrpc_invoke_ctx *ctx,int metalen) +static int fastrpc_get_payload_size (struct fastrpc_invoke_ctx *ctx,int metalen, +u64 *out_size) {u64 size =0 ;int oix;
@@ -1007,15 +1008,22 @@ static u64 fastrpc_get_payload_size(struct fastrpc_invoke_ctx *ctx, int metalen)int i = ctx->olaps[oix].raix;if (ctx->args[i].fd ==0 || ctx->args[i].fd ==-1 ) { +u64 len = ctx->olaps[oix].mend - + ctx->olaps[oix].mstart;if (ctx->olaps[oix].offset ==0 ) size =ALIGN (size,FASTRPC_ALIGN ); - size += (ctx->olaps[oix].mend - ctx->olaps[oix].mstart); +if (check_add_overflow (size, len, &size)) +return -EOVERFLOW ; } } -return size; +if (size >SIZE_MAX ) +return -EOVERFLOW ; + + *out_size = size; +return 0 ; }static int fastrpc_create_maps (struct fastrpc_invoke_ctx *ctx)
@@ -1068,7 +1076,9 @@ static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx) inbufs =REMOTE_SCALARS_INBUFS (ctx->sc); metalen =fastrpc_get_meta_size (ctx); - pkt_size =fastrpc_get_payload_size (ctx, metalen); + err =fastrpc_get_payload_size (ctx, metalen, &pkt_size); +if (err) +return err; err =fastrpc_create_maps (ctx);if (err)
Signature · 1 line
2.54.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Pre-existing issues:
- [Critical] Incomplete Integer Overflow Mitigation in Payload Size Calculation
- [High] Memory leak of fastrpc_map and dma_buf references for handle arguments
- [High] Fatal double-free and Use-After-Free in fastrpc_map_attach error paths --
commit a690f181cf4241d027f4e7ef1ae4948e3ce1e15b
Author: Yousef Alhouseen <[email protected]>
misc: fastrpc: reject overflowing invoke payload sizes
This patch adds overflow checks to fastrpc_get_payload_size() to prevent undersized buffer allocations during invoke argument serialization. It returns an error if the payload size calculation overflows or exceeds SIZE_MAX.
Quoted 17 lines
drivers/misc/fastrpc.c +2
-1@@ -972,7 +972,8 @@ static int fastrpc_get_meta_size(struct fastrpc_invoke_ctx *ctx)return size; } -static u64 fastrpc_get_payload_size (struct fastrpc_invoke_ctx *ctx,int metalen) +static int fastrpc_get_payload_size (struct fastrpc_invoke_ctx *ctx,int metalen, +u64 *out_size) {u64 size =0 ;int oix;@@ -982,15 +983,22 @@ static u64 fastrpc_get_payload_size(struct fastrpc_invoke_ctx *ctx, int metalen)int i = ctx->olaps[oix].raix;
drivers/misc/fastrpc.c static u64 fastrpc_get_payload_size(struct fastrpc_invoke_ctx *ctx, int metalen) · line 993
Quoted 10 lines
drivers/misc/fastrpc.c +4
-1if (ctx->args[i].fd ==0 || ctx->args[i].fd ==-1 ) { +u64 len = ctx->olaps[oix].mend - + ctx->olaps[oix].mstart;if (ctx->olaps[oix].offset ==0 ) size =ALIGN (size,FASTRPC_ALIGN ); - size += (ctx->olaps[oix].mend - ctx->olaps[oix].mstart); +if (check_add_overflow (size, len, &size)) +return -EOVERFLOW ;
[Severity: Critical]
This is a pre-existing issue, but does ALIGN(size, FASTRPC_ALIGN) silently wrap around to 0 if size is near U64_MAX?
If size wraps to 0, check_add_overflow(size, len, &size) will not detect an overflow.
Could this lead to an undersized allocation where fastrpc_get_args() later writes out-of-bounds when initializing metadata structures like rpra, list, and pages?
drivers/misc/fastrpc.c static u64 fastrpc_get_payload_size(struct fastrpc_invoke_ctx *ctx, int metalen) · line 1029
Quoted 12 lines
drivers/misc/fastrpc.c +5
-1} } -return size; +if (size >SIZE_MAX ) +return -EOVERFLOW ; + + *out_size = size; +return 0 ; }static int fastrpc_create_maps (struct fastrpc_invoke_ctx *ctx)
[Severity: High]
This isn't a bug introduced by this patch, but does fastrpc_context_free() leak maps and dma_buf references created for handle arguments?
fastrpc_create_maps() creates maps for all arguments up to ctx->nscalars, which includes handle arguments where i >= ctx->nbufs.
However, the teardown loop in fastrpc_context_free() only iterates up to ctx->nbufs:
fastrpc_context_free() {
...
for (i = 0; i < ctx->nbufs; i++)
fastrpc_map_put(ctx->maps[i]);
...
}Are the maps for i >= ctx->nbufs left attached to the fastrpc_user list indefinitely, allowing a user-space process to continuously leak memory per invoke?
[Severity: High]
This is a pre-existing issue, but can fastrpc_map_attach() trigger a double-free in its error handling paths?
If an error occurs after map->table has been assigned, the error path explicitly detaches and puts the dma_buf:
fastrpc_map_attach() {
...
map_err:
dma_buf_detach(map->buf, map->attach);
attach_err:
dma_buf_put(map->buf);
get_err:
fastrpc_map_put(map);
...
}However, fastrpc_map_put() eventually calls fastrpc_free_map(), which repeats the detach and put operations on the same pointers because map->table is set:
fastrpc_free_map() {
...
if (map->table) {
...
dma_buf_detach(map->buf, map->attach);
dma_buf_put(map->buf);
}
...
}Could this double-put cause a reference count underflow and a use-after-free of the DMA buffer?
Quoted 11 lines
drivers/misc/fastrpc.c +3
-1@@ -1043,7 +1051,9 @@ static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx) inbufs =REMOTE_SCALARS_INBUFS (ctx->sc); metalen =fastrpc_get_meta_size (ctx); - pkt_size =fastrpc_get_payload_size (ctx, metalen); + err =fastrpc_get_payload_size (ctx, metalen, &pkt_size); +if (err) +return err; err =fastrpc_create_maps (ctx);if (err)
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1