Skip to content

Commit

Permalink
efi: cper: fix scnprintf() use in cper_mem_err_location()
Browse files Browse the repository at this point in the history
The last two if-clauses fail to update n, so whatever they might have
written at &msg[n] would be cut off by the final nul-termination.

That nul-termination is redundant; scnprintf(), just like snprintf(),
guarantees a nul-terminated output buffer, provided the buffer size is
positive.

And there's no need to discount one byte from the initial buffer;
vsnprintf() expects to be given the full buffer size - it's not going
to write the nul-terminator one beyond the given (buffer, size) pair.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
  • Loading branch information
Rasmus Villemoes authored and Ard Biesheuvel committed Aug 27, 2021
1 parent e73f0f0 commit 5eff88d
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions drivers/firmware/efi/cper.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ static int cper_mem_err_location(struct cper_mem_err_compact *mem, char *msg)
return 0;

n = 0;
len = CPER_REC_LEN - 1;
len = CPER_REC_LEN;
if (mem->validation_bits & CPER_MEM_VALID_NODE)
n += scnprintf(msg + n, len - n, "node: %d ", mem->node);
if (mem->validation_bits & CPER_MEM_VALID_CARD)
Expand Down Expand Up @@ -258,13 +258,12 @@ static int cper_mem_err_location(struct cper_mem_err_compact *mem, char *msg)
n += scnprintf(msg + n, len - n, "responder_id: 0x%016llx ",
mem->responder_id);
if (mem->validation_bits & CPER_MEM_VALID_TARGET_ID)
scnprintf(msg + n, len - n, "target_id: 0x%016llx ",
mem->target_id);
n += scnprintf(msg + n, len - n, "target_id: 0x%016llx ",
mem->target_id);
if (mem->validation_bits & CPER_MEM_VALID_CHIP_ID)
scnprintf(msg + n, len - n, "chip_id: %d ",
mem->extended >> CPER_MEM_CHIP_ID_SHIFT);
n += scnprintf(msg + n, len - n, "chip_id: %d ",
mem->extended >> CPER_MEM_CHIP_ID_SHIFT);

msg[n] = '\0';
return n;
}

Expand Down

0 comments on commit 5eff88d

Please sign in to comment.