mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] [PATCH] handle AT_SYMLINK_NOFOLLOW
@ 2021-02-16 17:30 Khem Raj
  2021-02-16 17:53 ` [musl] " Rich Felker
  0 siblings, 1 reply; 2+ messages in thread
From: Khem Raj @ 2021-02-16 17:30 UTC (permalink / raw)
  To: musl; +Cc: dalias, Richard Purdie, Khem Raj

From: Richard Purdie <richard.purdie@linuxfoundation.org>

For faccessat(), AT_SYMLINK_NOFOLLOW is a supported flag by the
Linux kernel and musl should really handle it correctly rather
than return EINVAL. Noticed from code in systemd.

Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Signed-off-by: Khem Raj <raj.khem@gmail.com>
---
 src/unistd/faccessat.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/src/unistd/faccessat.c b/src/unistd/faccessat.c
index 8e8689c1..22c30bc6 100644
--- a/src/unistd/faccessat.c
+++ b/src/unistd/faccessat.c
@@ -9,6 +9,7 @@ struct ctx {
 	const char *filename;
 	int amode;
 	int p;
+	int flag;
 };
 
 static int checker(void *p)
@@ -18,7 +19,7 @@ static int checker(void *p)
 	if (__syscall(SYS_setregid, __syscall(SYS_getegid), -1)
 	    || __syscall(SYS_setreuid, __syscall(SYS_geteuid), -1))
 		__syscall(SYS_exit, 1);
-	ret = __syscall(SYS_faccessat, c->fd, c->filename, c->amode, 0);
+	ret = __syscall(SYS_faccessat, c->fd, c->filename, c->amode, c->flag & AT_SYMLINK_NOFOLLOW);
 	__syscall(SYS_write, c->p, &ret, sizeof ret);
 	return 0;
 }
@@ -30,11 +31,11 @@ int faccessat(int fd, const char *filename, int amode, int flag)
 		if (ret != -ENOSYS) return __syscall_ret(ret);
 	}
 
-	if (flag & ~AT_EACCESS)
+	if (flag & ~(AT_EACCESS | AT_SYMLINK_NOFOLLOW))
 		return __syscall_ret(-EINVAL);
 
-	if (!flag || (getuid()==geteuid() && getgid()==getegid()))
-		return syscall(SYS_faccessat, fd, filename, amode);
+	if (!(flag & AT_EACCESS) || (getuid()==geteuid() && getgid()==getegid()))
+		return syscall(SYS_faccessat, fd, filename, amode, flag);
 
 	char stack[1024];
 	sigset_t set;
@@ -42,7 +43,7 @@ int faccessat(int fd, const char *filename, int amode, int flag)
 	int ret, p[2];
 
 	if (pipe2(p, O_CLOEXEC)) return __syscall_ret(-EBUSY);
-	struct ctx c = { .fd = fd, .filename = filename, .amode = amode, .p = p[1] };
+	struct ctx c = { .fd = fd, .filename = filename, .amode = amode, .p = p[1], .flag=flag };
 
 	__block_all_sigs(&set);
 	
-- 
2.30.1


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

* [musl] Re: [PATCH] handle AT_SYMLINK_NOFOLLOW
  2021-02-16 17:30 [musl] [PATCH] handle AT_SYMLINK_NOFOLLOW Khem Raj
@ 2021-02-16 17:53 ` Rich Felker
  0 siblings, 0 replies; 2+ messages in thread
From: Rich Felker @ 2021-02-16 17:53 UTC (permalink / raw)
  To: musl

On Tue, Feb 16, 2021 at 09:30:22AM -0800, Khem Raj wrote:
> From: Richard Purdie <richard.purdie@linuxfoundation.org>
> 
> For faccessat(), AT_SYMLINK_NOFOLLOW is a supported flag by the
> Linux kernel and musl should really handle it correctly rather
> than return EINVAL. Noticed from code in systemd.
> 
> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
> Signed-off-by: Khem Raj <raj.khem@gmail.com>

This patch does not work. It just makes the error go away by silently
doing the wrong thing instead of reporting it.

> ---
>  src/unistd/faccessat.c | 11 ++++++-----
>  1 file changed, 6 insertions(+), 5 deletions(-)
> 
> diff --git a/src/unistd/faccessat.c b/src/unistd/faccessat.c
> index 8e8689c1..22c30bc6 100644
> --- a/src/unistd/faccessat.c
> +++ b/src/unistd/faccessat.c
> @@ -9,6 +9,7 @@ struct ctx {
>  	const char *filename;
>  	int amode;
>  	int p;
> +	int flag;
>  };
>  
>  static int checker(void *p)
> @@ -18,7 +19,7 @@ static int checker(void *p)
>  	if (__syscall(SYS_setregid, __syscall(SYS_getegid), -1)
>  	    || __syscall(SYS_setreuid, __syscall(SYS_geteuid), -1))
>  		__syscall(SYS_exit, 1);
> -	ret = __syscall(SYS_faccessat, c->fd, c->filename, c->amode, 0);
> +	ret = __syscall(SYS_faccessat, c->fd, c->filename, c->amode, c->flag & AT_SYMLINK_NOFOLLOW);

The SYS_faccessat syscall does not take a flags argument. That's the
whole reason for having this emulation mechanism. The 0 being left
there is a historical error and it should be removed; the kernel does
not inspect it and is not intended to sincw the old syscall has only 3
arguments.

>  	__syscall(SYS_write, c->p, &ret, sizeof ret);
>  	return 0;
>  }
> @@ -30,11 +31,11 @@ int faccessat(int fd, const char *filename, int amode, int flag)
>  		if (ret != -ENOSYS) return __syscall_ret(ret);
>  	}
>  
> -	if (flag & ~AT_EACCESS)
> +	if (flag & ~(AT_EACCESS | AT_SYMLINK_NOFOLLOW))
>  		return __syscall_ret(-EINVAL);

EINVAL is the normal error code Linux returns for flags not
recognized/supported by the running kernel. It's also the
POSIX-documented "may fail" code for this. The code *before* this
test, using the new SYS_faccessat2 syscall, handles the
AT_SYMLINK_NOFOLLOW flag if you have a kernel that can support it.

I suppose it might be possible to emulate AT_SYMLINK_NOFOLLOW on
old kernels using procfs magic symlinks, but I haven't checked the
details to be sure, and IMO it does not make sense to make the
fallback code here more complex when it's for a nonstandard feature
that's not expected to be present on old kernels, rather than a
POSIX-mandated feature like AT_EACCESS.

> -	if (!flag || (getuid()==geteuid() && getgid()==getegid()))
> -		return syscall(SYS_faccessat, fd, filename, amode);
> +	if (!(flag & AT_EACCESS) || (getuid()==geteuid() && getgid()==getegid()))
> +		return syscall(SYS_faccessat, fd, filename, amode, flag);

Same issue here -- there is no flag argument.

Rich

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

end of thread, other threads:[~2021-02-16 17:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-16 17:30 [musl] [PATCH] handle AT_SYMLINK_NOFOLLOW Khem Raj
2021-02-16 17:53 ` [musl] " Rich Felker

Code repositories for project(s) associated with this public 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).