Bug 107344 - fcntl(fd, F_SETOWN, getpid()) fails with EPERM in a NPTL thread
fcntl(fd, F_SETOWN, getpid()) fails with EPERM in a NPTL thread
Product: Red Hat Linux
Classification: Retired
Component: kernel (Show other bugs)
i686 Linux
medium Severity medium
: ---
: ---
Assigned To: Arjan van de Ven
Brian Brock
Depends On:
  Show dependency treegraph
Reported: 2003-10-17 00:42 EDT by David Holmes
Modified: 2007-04-18 12:58 EDT (History)
3 users (show)

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2004-09-30 11:41:36 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)

  None (edit)
Description David Holmes 2003-10-17 00:43:00 EDT
From Bugzilla Helper:
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20030225

Description of problem:
Using RH9 out-of-the-box with the NPTL 0.34 update applied.
The test program tries to do fcntl(fd, F_SETOWN, getpid()) on a socket
descriptor. If it does this in the main thread then it works fine. If it does
this in a separate pthread then it fails with EPERM.
Looking into the bowels of sock.c sock_no_fcntl does this:
   case F_SETOWN:
        if (current->pgrp != -arg &&
            current->pid != arg &&
            !capable(CAP_KILL)) return(-EPERM);
Running as root provides the CAP_KILL capability and the test passes. So it
seems that there is a discrepancy between the value returned by
getpid() and the value used internally (current->pid).
This only fails with NPTL. If I use LD_ASSUME_KERNEL the test passes
threaded and non-threaded.

I reported this on phil_list and  Roland McGrath responded that this "is a
kernel bug that needs to be fixed in the RH backport of the NPTL support code"
and that I should file this bug report.

Version-Release number of selected component (if applicable):

How reproducible:

Steps to Reproduce:
1. Compile the test program.

2. Run with noargs uses a non-thread version and fcntl succeeds.
3. Run with any arg a the threaded version runs and fcntl fails with EPERM.    

----- cut here for test program fcntlbug.c -----
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#define CHECK(func, msg) \
   if ((func) != 0) { \
      fprintf(stderr, "Error: %s - %s\n", msg, strerror(errno)); \
      exit(1); \
#define CHECKP(func) \
   do { \
    int rc = (func); \
    if (rc!= 0) { \
      fprintf(stderr, "Error: " #func " - %s\n", strerror(rc)); \
      exit(1); \
    } \
   } while(0)
int fds[] = { -1, -1 };
static int fcntlSetFlag(int fd,int flag) {
        int flags=fcntl(fd,F_GETFL,0);
        if (flags<0) {
                return -1;
        return fcntl(fd,F_SETFL,flags|flag);
static void test() {
    int fd;
    CHECK(socketpair(AF_UNIX, SOCK_STREAM, 0, fds), "socketpair");
    fd = fds[0];
    if (fcntlSetFlag(fd, O_ASYNC)<0) {
        fprintf(stderr, "error setting O_ASYNC flag on %d: %s\n",
               fd, strerror(errno));
    if (fcntl(fd, F_SETOWN, getpid())<0) {
        fprintf(stderr, "error fcntl(%d, F_SETOWN, %d): %s\n",fd, getpid(),
    printf("Managed to set up socket %d ok\n", fd);
static void* threadRun(void* arg) {
    pthread_detach(pthread_self()); /* ensure detached to release resources */
static void threadedTest() {
    pthread_t thread;
    pthread_attr_t attr;
    /* we take default status:
       - joinable
       - default contention scope  ??
       - default stack attributes
    /* always set this so that the thread uses the attr object we pass in */
    CHECKP(pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED));
    CHECKP(pthread_create(&thread, &attr, threadRun, 0));
int main(int argc, char* argv[]) {
    if (argc > 1) {
        printf("Doing threaded test\n");
    else {
        printf("Doing normal test\n");
    return 0;

Actual Results:  2. hyperT ~ > ./fcntlbug
Doing normal test
Managed to set up socket 3 ok

3. hyperT ~ > ./fcntlbug t
Doing threaded test
error fcntl(3, F_SETOWN, 28525): Operation not permitted

Expected Results:  3. hyperT ~ > ./fcntlbug t
Doing threaded test
Managed to set up socket 3 ok

Additional info:
Comment 1 Roland McGrath 2003-10-22 03:12:28 EDT
I have a draft kernel patch we will try out.
Comment 2 Bugzilla owner 2004-09-30 11:41:36 EDT
Thanks for the bug report. However, Red Hat no longer maintains this version of
the product. Please upgrade to the latest version and open a new bug if the problem

The Fedora Legacy project (http://fedoralegacy.org/) maintains some older releases, 
and if you believe this bug is interesting to them, please report the problem in
the bug tracker at: http://bugzilla.fedora.us/

Note You need to log in before you can comment on or make changes to this bug.