[glibc][PATCH] nptl: open threads comm with O_WRONLY|O_CLOEXEC in pthread_setname_np

Sana Kazi git-patches@bmwtechworks.in
Wed Jun 10 05:17:53 GMT 2026


From: Sana Kazi <Sana.Kazi@bmwtechworks.in>

pthread_setname_np opens the thread's comm file using O_RDWR, but the
function only ever writes to it.  This causes two distinct problems:

1. Missing O_CLOEXEC: the file descriptor is not marked close-on-exec,
   so it remains open across fork+exec.  A child process that audits
   its inherited file-descriptor set will encounter an unexpected /proc
   fd it did not open and may treat this as a security violation and
   abort.

2. Unnecessary O_RDWR: requesting read+write access when only write
   access is needed can cause open() to fail under security policies
   that permit writing to /proc/<tid>/comm but deny reading it.

Fix both issues by replacing O_RDWR with O_WRONLY|O_CLOEXEC

Signed-off-by: Sana Kazi <Sana.Kazi@bmwtechworks.in>
---
 nptl/pthread_setname.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/nptl/pthread_setname.c b/nptl/pthread_setname.c
index 62f4964fcc..f9a528c3d8 100644
--- a/nptl/pthread_setname.c
+++ b/nptl/pthread_setname.c
@@ -46,7 +46,7 @@ __pthread_setname_np (pthread_t th, const char *name)
   char fname[sizeof (FMT) + 8];
   sprintf (fname, FMT, (unsigned int) pd->tid);
 
-  int fd = __open64_nocancel (fname, O_RDWR);
+  int fd = __open64_nocancel (fname, O_WRONLY | O_CLOEXEC);
   if (fd == -1)
     return errno;
 
-- 
2.43.0



More information about the Libc-alpha mailing list