* [PATCH] libselinux: rename gettid() to something which never conflicts with the libc
@ 2021-02-16 21:13 Nicolas Iooss
2021-02-18 11:26 ` Petr Lautrbach
0 siblings, 1 reply; 3+ messages in thread
From: Nicolas Iooss @ 2021-02-16 21:13 UTC (permalink / raw)
To: selinux
Musl recently added a wrapper for gettid() syscall. There is no way to
detect this new version in a reliable way, so rename our gettid()
wrapper to a non-conflicting name.
Introduce a new function which, when using a libc known to provide a
wrapper for gettid(), calls it, and which, otherwise, performs the
syscall directly.
Anyway this function is only used on systems where /proc/thread-self
does not exist, which are therefore running Linux<3.17.
Fixes: https://github.com/SELinuxProject/selinux/issues/282
Signed-off-by: Nicolas Iooss <nicolas.iooss@m4x.org>
diff --git a/libselinux/src/procattr.c b/libselinux/src/procattr.c
index 1aa67ac53f39..840570525f5f 100644
--- a/libselinux/src/procattr.c
+++ b/libselinux/src/procattr.c
@@ -25,21 +25,23 @@ static __thread char destructor_initialized;
/* Bionic and glibc >= 2.30 declare gettid() system call wrapper in unistd.h and
* has a definition for it */
#ifdef __BIONIC__
- #define OVERRIDE_GETTID 0
+ #define HAVE_GETTID 1
#elif !defined(__GLIBC_PREREQ)
- #define OVERRIDE_GETTID 1
+ #define HAVE_GETTID 0
#elif !__GLIBC_PREREQ(2,30)
- #define OVERRIDE_GETTID 1
+ #define HAVE_GETTID 0
#else
- #define OVERRIDE_GETTID 0
+ #define HAVE_GETTID 1
#endif
-#if OVERRIDE_GETTID
-static pid_t gettid(void)
+static pid_t selinux_gettid(void)
{
+#if HAVE_GETTID
+ return gettid();
+#else
return syscall(__NR_gettid);
-}
#endif
+}
static void procattr_thread_destructor(void __attribute__((unused)) *unused)
{
@@ -94,7 +96,7 @@ static int openattr(pid_t pid, const char *attr, int flags)
if (fd >= 0 || errno != ENOENT)
goto out;
free(path);
- tid = gettid();
+ tid = selinux_gettid();
rc = asprintf(&path, "/proc/self/task/%d/attr/%s", tid, attr);
} else {
errno = EINVAL;
--
2.30.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] libselinux: rename gettid() to something which never conflicts with the libc
2021-02-16 21:13 [PATCH] libselinux: rename gettid() to something which never conflicts with the libc Nicolas Iooss
@ 2021-02-18 11:26 ` Petr Lautrbach
2021-02-19 15:17 ` Petr Lautrbach
0 siblings, 1 reply; 3+ messages in thread
From: Petr Lautrbach @ 2021-02-18 11:26 UTC (permalink / raw)
To: Nicolas Iooss, selinux
Nicolas Iooss <nicolas.iooss@m4x.org> writes:
> Musl recently added a wrapper for gettid() syscall. There is no way to
> detect this new version in a reliable way, so rename our gettid()
> wrapper to a non-conflicting name.
>
> Introduce a new function which, when using a libc known to provide a
> wrapper for gettid(), calls it, and which, otherwise, performs the
> syscall directly.
>
> Anyway this function is only used on systems where /proc/thread-self
> does not exist, which are therefore running Linux<3.17.
>
> Fixes: https://github.com/SELinuxProject/selinux/issues/282
> Signed-off-by: Nicolas Iooss <nicolas.iooss@m4x.org>
Tested on Fedora with musl-1.2.22 scratch build
https://koji.fedoraproject.org/koji/taskinfo?taskID=62214131 :
Before
^&^ musl-gcc -D_GNU_SOURCE -I../include procattr.c -c
procattr.c:38:14: error: static declaration of ‘gettid’ follows non-static declaration
38 | static pid_t gettid(void)
| ^~~~~~
In file included from procattr.c:2:
/usr/x86_64-linux-musl/include/unistd.h:194:7: note: previous declaration of ‘gettid’ with type ‘pid_t(void)’ {aka ‘int(void)’}
194 | pid_t gettid(void);
| ^~~~~~
After
^&^ musl-gcc -D_GNU_SOURCE -I../include procattr.c -c
Seems to work. Thanks!
Acked-by: Petr Lautrbach <plautrba@redhat.com>
> diff --git a/libselinux/src/procattr.c b/libselinux/src/procattr.c
> index 1aa67ac53f39..840570525f5f 100644
> --- a/libselinux/src/procattr.c
> +++ b/libselinux/src/procattr.c
> @@ -25,21 +25,23 @@ static __thread char destructor_initialized;
> /* Bionic and glibc >= 2.30 declare gettid() system call wrapper in unistd.h and
> * has a definition for it */
> #ifdef __BIONIC__
> - #define OVERRIDE_GETTID 0
> + #define HAVE_GETTID 1
> #elif !defined(__GLIBC_PREREQ)
> - #define OVERRIDE_GETTID 1
> + #define HAVE_GETTID 0
> #elif !__GLIBC_PREREQ(2,30)
> - #define OVERRIDE_GETTID 1
> + #define HAVE_GETTID 0
> #else
> - #define OVERRIDE_GETTID 0
> + #define HAVE_GETTID 1
> #endif
>
> -#if OVERRIDE_GETTID
> -static pid_t gettid(void)
> +static pid_t selinux_gettid(void)
> {
> +#if HAVE_GETTID
> + return gettid();
> +#else
> return syscall(__NR_gettid);
> -}
> #endif
> +}
>
> static void procattr_thread_destructor(void __attribute__((unused)) *unused)
> {
> @@ -94,7 +96,7 @@ static int openattr(pid_t pid, const char *attr, int flags)
> if (fd >= 0 || errno != ENOENT)
> goto out;
> free(path);
> - tid = gettid();
> + tid = selinux_gettid();
> rc = asprintf(&path, "/proc/self/task/%d/attr/%s", tid, attr);
> } else {
> errno = EINVAL;
> --
> 2.30.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] libselinux: rename gettid() to something which never conflicts with the libc
2021-02-18 11:26 ` Petr Lautrbach
@ 2021-02-19 15:17 ` Petr Lautrbach
0 siblings, 0 replies; 3+ messages in thread
From: Petr Lautrbach @ 2021-02-19 15:17 UTC (permalink / raw)
To: Nicolas Iooss, selinux
Petr Lautrbach <plautrba@redhat.com> writes:
> Nicolas Iooss <nicolas.iooss@m4x.org> writes:
>
>> Musl recently added a wrapper for gettid() syscall. There is no way to
>> detect this new version in a reliable way, so rename our gettid()
>> wrapper to a non-conflicting name.
>>
>> Introduce a new function which, when using a libc known to provide a
>> wrapper for gettid(), calls it, and which, otherwise, performs the
>> syscall directly.
>>
>> Anyway this function is only used on systems where /proc/thread-self
>> does not exist, which are therefore running Linux<3.17.
>>
>> Fixes: https://github.com/SELinuxProject/selinux/issues/282
>> Signed-off-by: Nicolas Iooss <nicolas.iooss@m4x.org>
>
> Tested on Fedora with musl-1.2.22 scratch build
> https://koji.fedoraproject.org/koji/taskinfo?taskID=62214131 :
>
> Before
> ^&^ musl-gcc -D_GNU_SOURCE -I../include procattr.c -c
> procattr.c:38:14: error: static declaration of ‘gettid’ follows non-static declaration
> 38 | static pid_t gettid(void)
> | ^~~~~~
> In file included from procattr.c:2:
> /usr/x86_64-linux-musl/include/unistd.h:194:7: note: previous declaration of ‘gettid’ with type ‘pid_t(void)’ {aka ‘int(void)’}
> 194 | pid_t gettid(void);
> | ^~~~~~
>
>
> After
> ^&^ musl-gcc -D_GNU_SOURCE -I../include procattr.c -c
>
>
> Seems to work. Thanks!
>
> Acked-by: Petr Lautrbach <plautrba@redhat.com>
>
Merged.
>
>> diff --git a/libselinux/src/procattr.c b/libselinux/src/procattr.c
>> index 1aa67ac53f39..840570525f5f 100644
>> --- a/libselinux/src/procattr.c
>> +++ b/libselinux/src/procattr.c
>> @@ -25,21 +25,23 @@ static __thread char destructor_initialized;
>> /* Bionic and glibc >= 2.30 declare gettid() system call wrapper in unistd.h and
>> * has a definition for it */
>> #ifdef __BIONIC__
>> - #define OVERRIDE_GETTID 0
>> + #define HAVE_GETTID 1
>> #elif !defined(__GLIBC_PREREQ)
>> - #define OVERRIDE_GETTID 1
>> + #define HAVE_GETTID 0
>> #elif !__GLIBC_PREREQ(2,30)
>> - #define OVERRIDE_GETTID 1
>> + #define HAVE_GETTID 0
>> #else
>> - #define OVERRIDE_GETTID 0
>> + #define HAVE_GETTID 1
>> #endif
>>
>> -#if OVERRIDE_GETTID
>> -static pid_t gettid(void)
>> +static pid_t selinux_gettid(void)
>> {
>> +#if HAVE_GETTID
>> + return gettid();
>> +#else
>> return syscall(__NR_gettid);
>> -}
>> #endif
>> +}
>>
>> static void procattr_thread_destructor(void __attribute__((unused)) *unused)
>> {
>> @@ -94,7 +96,7 @@ static int openattr(pid_t pid, const char *attr, int flags)
>> if (fd >= 0 || errno != ENOENT)
>> goto out;
>> free(path);
>> - tid = gettid();
>> + tid = selinux_gettid();
>> rc = asprintf(&path, "/proc/self/task/%d/attr/%s", tid, attr);
>> } else {
>> errno = EINVAL;
>> --
>> 2.30.0
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2021-02-19 15:19 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-02-16 21:13 [PATCH] libselinux: rename gettid() to something which never conflicts with the libc Nicolas Iooss
2021-02-18 11:26 ` Petr Lautrbach
2021-02-19 15:17 ` Petr Lautrbach
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox