linux-trace-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] bootconfig: Fix negative seeks on 32-bit with LFS enabled
@ 2025-08-17 14:21 Ben Hutchings
  2025-08-20  1:07 ` Masami Hiramatsu
  0 siblings, 1 reply; 2+ messages in thread
From: Ben Hutchings @ 2025-08-17 14:21 UTC (permalink / raw)
  To: Masami Hiramatsu; +Cc: linux-kernel, linux-trace-kernel

[-- Attachment #1: Type: text/plain, Size: 1726 bytes --]

Commit 26dda5769509 "tools/bootconfig: Cleanup bootconfig footer size
calculations" replaced some expressions of type int with the
BOOTCONFIG_FOOTER_SIZE macro, which expands to an expression of type
size_t, which is unsigned.

On 32-bit architectures with LFS enabled (i.e. off_t is 64-bit), the
seek offset of -BOOTCONFIG_FOOTER_SIZE now turns into a positive
value.

Fix this by casting the size to off_t before negating it.

Just in case someone changes BOOTCONFIG_MAGIC_LEN to have type size_t
later, do the same thing to the seek offset of -BOOTCONFIG_MAGIC_LEN.

Fixes: 26dda5769509 ("tools/bootconfig: Cleanup bootconfig footer size calculations")
Signed-off-by: Ben Hutchings <benh@debian.org>
---
 tools/bootconfig/main.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/bootconfig/main.c b/tools/bootconfig/main.c
index 57c669d2aa90..55d59ed507d5 100644
--- a/tools/bootconfig/main.c
+++ b/tools/bootconfig/main.c
@@ -193,7 +193,7 @@ static int load_xbc_from_initrd(int fd, char **buf)
 	if (stat.st_size < BOOTCONFIG_FOOTER_SIZE)
 		return 0;
 
-	if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0)
+	if (lseek(fd, -(off_t)BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0)
 		return pr_errno("Failed to lseek for magic", -errno);
 
 	if (read(fd, magic, BOOTCONFIG_MAGIC_LEN) < 0)
@@ -203,7 +203,7 @@ static int load_xbc_from_initrd(int fd, char **buf)
 	if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0)
 		return 0;
 
-	if (lseek(fd, -BOOTCONFIG_FOOTER_SIZE, SEEK_END) < 0)
+	if (lseek(fd, -(off_t)BOOTCONFIG_FOOTER_SIZE, SEEK_END) < 0)
 		return pr_errno("Failed to lseek for size", -errno);
 
 	if (read(fd, &size, sizeof(uint32_t)) < 0)

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] bootconfig: Fix negative seeks on 32-bit with LFS enabled
  2025-08-17 14:21 [PATCH] bootconfig: Fix negative seeks on 32-bit with LFS enabled Ben Hutchings
@ 2025-08-20  1:07 ` Masami Hiramatsu
  0 siblings, 0 replies; 2+ messages in thread
From: Masami Hiramatsu @ 2025-08-20  1:07 UTC (permalink / raw)
  To: Ben Hutchings; +Cc: linux-kernel, linux-trace-kernel

On Sun, 17 Aug 2025 16:21:46 +0200
Ben Hutchings <benh@debian.org> wrote:

> Commit 26dda5769509 "tools/bootconfig: Cleanup bootconfig footer size
> calculations" replaced some expressions of type int with the
> BOOTCONFIG_FOOTER_SIZE macro, which expands to an expression of type
> size_t, which is unsigned.
> 
> On 32-bit architectures with LFS enabled (i.e. off_t is 64-bit), the
> seek offset of -BOOTCONFIG_FOOTER_SIZE now turns into a positive
> value.

Oops, I thought the sign bit would be extended.

> 
> Fix this by casting the size to off_t before negating it.
> 
> Just in case someone changes BOOTCONFIG_MAGIC_LEN to have type size_t
> later, do the same thing to the seek offset of -BOOTCONFIG_MAGIC_LEN.
> 
> Fixes: 26dda5769509 ("tools/bootconfig: Cleanup bootconfig footer size calculations")
> Signed-off-by: Ben Hutchings <benh@debian.org>

Thanks for the fix!

> ---
>  tools/bootconfig/main.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/bootconfig/main.c b/tools/bootconfig/main.c
> index 57c669d2aa90..55d59ed507d5 100644
> --- a/tools/bootconfig/main.c
> +++ b/tools/bootconfig/main.c
> @@ -193,7 +193,7 @@ static int load_xbc_from_initrd(int fd, char **buf)
>  	if (stat.st_size < BOOTCONFIG_FOOTER_SIZE)
>  		return 0;
>  
> -	if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0)
> +	if (lseek(fd, -(off_t)BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0)
>  		return pr_errno("Failed to lseek for magic", -errno);
>  
>  	if (read(fd, magic, BOOTCONFIG_MAGIC_LEN) < 0)
> @@ -203,7 +203,7 @@ static int load_xbc_from_initrd(int fd, char **buf)
>  	if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0)
>  		return 0;
>  
> -	if (lseek(fd, -BOOTCONFIG_FOOTER_SIZE, SEEK_END) < 0)
> +	if (lseek(fd, -(off_t)BOOTCONFIG_FOOTER_SIZE, SEEK_END) < 0)
>  		return pr_errno("Failed to lseek for size", -errno);
>  
>  	if (read(fd, &size, sizeof(uint32_t)) < 0)


-- 
Masami Hiramatsu (Google) <mhiramat@kernel.org>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-08-20  1:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-17 14:21 [PATCH] bootconfig: Fix negative seeks on 32-bit with LFS enabled Ben Hutchings
2025-08-20  1:07 ` Masami Hiramatsu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).