Skip to contentSkip to conversation

amdgpu: make sure to set CLOEXEC on duplicated FDs

PATCH In review Christian König 3 participants 3 messages Jul 17, 2018 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 amdgpu: make sure to set CLOEXEC on duplicated FDs

Otherwise we leak file descriptors into child processes.

---
 amdgpu/amdgpu_device.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
amdgpu/amdgpu_device.c +3 -2
index 34ac95b8..d7aec6a4 100644
@@ -34,6 +34,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <fcntl.h>
 
 #include "xf86drm.h"
 #include "amdgpu_drm.h"
@@ -205,7 +206,7 @@ int amdgpu_device_initialize(int fd,
 			return r;
 		}
 		if ((flag_auth) && (!flag_authexist)) {
-			dev->flink_fd = dup(fd);
+			dev->flink_fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
 		}
 		*major_version = dev->major_version;
 		*minor_version = dev->minor_version;
@@ -239,7 +240,7 @@ int amdgpu_device_initialize(int fd,
 		goto cleanup;
 	}
 
-	dev->fd = dup(fd);
+	dev->fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
 	dev->flink_fd = dev->fd;
 	dev->major_version = version->version_major;
 	dev->minor_version = version->version_minor;
Signature · 6 lines
2.14.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#2 lore raw
In reply to a message not in the archive
Quoted 3 lines · Christian König

Otherwise we leak file descriptors into child processes.

Yeah, that's the key point that dup could remove CLOEXEC effect.

Reviewed-and-Tested-by Junwei Zhang <[email protected]>
Quoted 27 lines
---
  amdgpu/amdgpu_device.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
amdgpu/amdgpu_device.c +2 -1
index 34ac95b8..d7aec6a4 100644
@@ -34,6 +34,7 @@
  #include <stdio.h>
  #include <stdlib.h>
  #include <unistd.h>
+#include <fcntl.h>
 
  #include "xf86drm.h"
  #include "amdgpu_drm.h"
@@ -205,7 +206,7 @@ int amdgpu_device_initialize(int fd,
  			return r;
  		}
  		if ((flag_auth) && (!flag_authexist)) {
-			dev->flink_fd = dup(fd);
+			dev->flink_fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
  		}
  		*major_version = dev->major_version;
  		*minor_version = dev->minor_version;

amdgpu/amdgpu_device.c int amdgpu_device_initialize(int fd, · line 246

Quoted 8 lines
amdgpu/amdgpu_device.c +1 -1
@@ -239,7 +240,7 @@ int amdgpu_device_initialize(int fd,
  		goto cleanup;
  	}
 
-	dev->fd = dup(fd);
+	dev->fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
  	dev->flink_fd = dev->fd;
  	dev->major_version = version->version_major;
  	dev->minor_version = version->version_minor;

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

Reviewed-by
#3 lore raw
In reply to a message not in the archive
Quoted 30 lines · Christian König

Otherwise we leak file descriptors into child processes.

---
 amdgpu/amdgpu_device.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
amdgpu/amdgpu_device.c +2 -1
index 34ac95b8..d7aec6a4 100644
@@ -34,6 +34,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <fcntl.h>
 
 #include "xf86drm.h"
 #include "amdgpu_drm.h"
@@ -205,7 +206,7 @@ int amdgpu_device_initialize(int fd,
 			return r;
 		}
 		if ((flag_auth) && (!flag_authexist)) {
-			dev->flink_fd = dup(fd);
+			dev->flink_fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
 		}
 		*major_version = dev->major_version;
 		*minor_version = dev->minor_version;

amdgpu/amdgpu_device.c int amdgpu_device_initialize(int fd, · line 246

Quoted 8 lines
amdgpu/amdgpu_device.c +1 -1
@@ -239,7 +240,7 @@ int amdgpu_device_initialize(int fd,
 		goto cleanup;
 	}
 
-	dev->fd = dup(fd);
+	dev->fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
 	dev->flink_fd = dev->fd;
 	dev->major_version = version->version_major;
 	dev->minor_version = version->version_minor;

Nice catch.

Signature · 6 lines
Earthling Michel Dänzer               |               http://www.amd.com
Libre software enthusiast             |             Mesa and X developer
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx