mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] BUG fix: mmap pass wrong offset to kernel
@ 2021-11-16  3:56 Minqiang Chen (ptpt52)
  2021-11-16 16:18 ` Rich Felker
  0 siblings, 1 reply; 2+ messages in thread
From: Minqiang Chen (ptpt52) @ 2021-11-16  3:56 UTC (permalink / raw)
  To: musl

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

From 146066a9794b8e39c53337b71a8476b86e79e7d4 Mon Sep 17 00:00:00 2001
From: Chen Minqiang <ptpt52@gmail.com>
Date: Mon, 16 Oct 2017 08:57:41 +0800
Subject: [PATCH] musl: fix mmap pass wrong offset to kernel

on 32bit platform for example off_t x=0x8d9eb000, the x/4096 result
is 0xfff8d9eb, but the sys_mmap2() is expecting 0x8d9eb to be pass to

this happens on 32bit platform or 64bit platform when
x > = 0x80000000 (32bit platform)
or
x > = 0x8000000000000000 (64bit platform)

Signed-off-by: Chen Minqiang <ptpt52@gmail.com>
---
 src/mman/mmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/mman/mmap.c b/src/mman/mmap.c
index eff88d82..f225cdbb 100644
--- a/src/mman/mmap.c
+++ b/src/mman/mmap.c
@@ -26,7 +26,7 @@ void *__mmap(void *start, size_t len, int prot, int flags, int fd, off_t off)
                __vm_wait();
        }
 #ifdef SYS_mmap2
-       ret = __syscall(SYS_mmap2, start, len, prot, flags, fd, off/UNIT);
+       ret = __syscall(SYS_mmap2, start, len, prot, flags, fd, (unsigned long)off/UNIT);
 #else
        ret = __syscall(SYS_mmap, start, len, prot, flags, fd, off);
 #endif
-- 
2.17.1


[-- Attachment #2: Type: text/html, Size: 9119 bytes --]

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

* Re: [musl] BUG fix: mmap pass wrong offset to kernel
  2021-11-16  3:56 [musl] BUG fix: mmap pass wrong offset to kernel Minqiang Chen (ptpt52)
@ 2021-11-16 16:18 ` Rich Felker
  0 siblings, 0 replies; 2+ messages in thread
From: Rich Felker @ 2021-11-16 16:18 UTC (permalink / raw)
  To: Minqiang Chen (ptpt52); +Cc: musl

On Tue, Nov 16, 2021 at 11:56:57AM +0800, Minqiang Chen (ptpt52) wrote:
> From 146066a9794b8e39c53337b71a8476b86e79e7d4 Mon Sep 17 00:00:00 2001
> From: Chen Minqiang <ptpt52@gmail.com>
> Date: Mon, 16 Oct 2017 08:57:41 +0800
> Subject: [PATCH] musl: fix mmap pass wrong offset to kernel
> 
> on 32bit platform for example off_t x=0x8d9eb000, the x/4096 result
> is 0xfff8d9eb, but the sys_mmap2() is expecting 0x8d9eb to be pass to
> 
> this happens on 32bit platform or 64bit platform when
> x > = 0x80000000 (32bit platform)
> or
> x > = 0x8000000000000000 (64bit platform)
> 
> Signed-off-by: Chen Minqiang <ptpt52@gmail.com>
> ---
>  src/mman/mmap.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/src/mman/mmap.c b/src/mman/mmap.c
> index eff88d82..f225cdbb 100644
> --- a/src/mman/mmap.c
> +++ b/src/mman/mmap.c
> @@ -26,7 +26,7 @@ void *__mmap(void *start, size_t len, int prot, int flags, int fd, off_t off)
>                 __vm_wait();
>         }
>  #ifdef SYS_mmap2
> -       ret = __syscall(SYS_mmap2, start, len, prot, flags, fd, off/UNIT);
> +       ret = __syscall(SYS_mmap2, start, len, prot, flags, fd, (unsigned long)off/UNIT);
>  #else
>         ret = __syscall(SYS_mmap, start, len, prot, flags, fd, off);
>  #endif
> -- 
> 2.17.1

This patch is wrong and truncates offsets over 32-bit (drops all the
high bits). There is a bug here, but it's just that UNIT has the wrong
type. commit b5bbe797493ea732d4cac15619753c545ed392af introduced the
regression by making UNIT have type unsigned long long. It should have
a small signed type; just int is fine.

Rich

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

end of thread, other threads:[~2021-11-16 16:18 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-16  3:56 [musl] BUG fix: mmap pass wrong offset to kernel Minqiang Chen (ptpt52)
2021-11-16 16:18 ` Rich Felker

Code repositories for project(s) associated with this inbox:

	https://git.vuxu.org/mirror/musl/

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).