Skip to contentSkip to conversation

kdbus: use LSM hooks to restrict ability to send file descriptors

RFC In review Paul Osmialowski 2 participants 2 messages Sep 21, 2015

lore
Share & tools
lore
#0 lore raw

RFC v2 1/1 kdbus: use LSM hooks to restrict ability to send file descriptors

The goal of this patch is to reproduce on kdbus the same behavior that is expressed by Unix Domain Sockets when it comes to restricting ability to pass opened file descriptors.

---
 ipc/kdbus/message.c | 31 ++++++++++++++++++++++---------
 1 file changed, 22 insertions(+), 9 deletions(-)
ipc/kdbus/message.c +22 -9
index ae565cd..7f8aa35 100644
@@ -24,6 +24,7 @@
 #include <linux/sizes.h>
 #include <linux/slab.h>
 #include <linux/uaccess.h>
+#include <linux/security.h>
 #include <net/sock.h>
 
 #include "bus.h"
@@ -150,13 +151,19 @@ int kdbus_gaps_install(struct kdbus_gaps *gaps, struct kdbus_pool_slice *slice,
 		for (i = 0; i < gaps->n_fds; ++i) {
 			int fd;
 
-			fd = get_unused_fd_flags(O_CLOEXEC);
-			if (fd < 0)
-				incomplete_fds = true;
-
 			WARN_ON(!gaps->fd_files[i]);
 
-			fds[n_fds++] = fd < 0 ? -1 : fd;
+			if (gaps->fd_files[i] &&
+				    security_file_receive(gaps->fd_files[i])) {
+				incomplete_fds = true;
+				fds[n_fds++] = -1;
+			} else {
+				fd = get_unused_fd_flags(O_CLOEXEC);
+				if (fd < 0)
+					incomplete_fds = true;
+
+				fds[n_fds++] = fd < 0 ? -1 : fd;
+			}
 		}
 
 		/*
@@ -178,6 +185,16 @@ int kdbus_gaps_install(struct kdbus_gaps *gaps, struct kdbus_pool_slice *slice,
 	for (i = 0; i < gaps->n_memfds; ++i) {
 		int memfd;
 
+		WARN_ON(!gaps->memfd_offsets[i]);
+		WARN_ON(!gaps->memfd_files[i]);
+
+		if (gaps->memfd_files[i] &&
+			    security_file_receive(gaps->memfd_files[i])) {
+			incomplete_fds = true;
+			fds[n_fds++] = -1;
+			continue;
+		}
+
 		memfd = get_unused_fd_flags(O_CLOEXEC);
 		if (memfd < 0) {
 			incomplete_fds = true;
@@ -193,10 +210,6 @@ int kdbus_gaps_install(struct kdbus_gaps *gaps, struct kdbus_pool_slice *slice,
 		 * as usually there is no need to send more than one memfd per
 		 * message.
 		 */
-
-		WARN_ON(!gaps->memfd_offsets[i]);
-		WARN_ON(!gaps->memfd_files[i]);
-
 		kvec.iov_base = &memfd;
 		kvec.iov_len = sizeof(memfd);
 		ret = kdbus_pool_slice_copy_kvec(slice, gaps->memfd_offsets[i],
Signature · 1 line
1.9.1
#1 lore raw

Hi

Quoted 23 lines · Paul Osmialowski

The goal of this patch is to reproduce on kdbus the same behavior that is expressed by Unix Domain Sockets when it comes to restricting ability to pass opened file descriptors.

---
 ipc/kdbus/message.c | 31 ++++++++++++++++++++++---------
 1 file changed, 22 insertions(+), 9 deletions(-)
ipc/kdbus/message.c +1 -0
index ae565cd..7f8aa35 100644
@@ -24,6 +24,7 @@
 #include <linux/sizes.h>
 #include <linux/slab.h>
 #include <linux/uaccess.h>
+#include <linux/security.h>
 #include <net/sock.h>
 
 #include "bus.h"
@@ -150,13 +151,19 @@ int kdbus_gaps_install(struct kdbus_gaps *gaps, struct kdbus_pool_slice *slice,
                for (i = 0; i < gaps->n_fds; ++i) {

ipc/kdbus/message.c int kdbus_gaps_install(struct kdbus_gaps *gaps, struct kdbus_pool_slice *slice, · line 156

Quoted 10 lines
ipc/kdbus/message.c +1 -5
                        int fd;
 
-                       fd = get_unused_fd_flags(O_CLOEXEC);
-                       if (fd < 0)
-                               incomplete_fds = true;
-
                        WARN_ON(!gaps->fd_files[i]);
 
-                       fds[n_fds++] = fd < 0 ? -1 : fd;
+                       if (gaps->fd_files[i] &&

Please drop the WARN_ON above and this condition. It was fine before, but here it just makes the code more complex, unnecessarily.

Quoted 14 lines
ipc/kdbus/message.c +10 -0
+                                   security_file_receive(gaps->fd_files[i])) {
+                               incomplete_fds = true;
+                               fds[n_fds++] = -1;
+                       } else {
+                               fd = get_unused_fd_flags(O_CLOEXEC);
+                               if (fd < 0)
+                                       incomplete_fds = true;
+
+                               fds[n_fds++] = fd < 0 ? -1 : fd;
+                       }
                }
 
                /*

ipc/kdbus/message.c int kdbus_gaps_install(struct kdbus_gaps *gaps, struct kdbus_pool_slice *slice, · line 191

Quoted 7 lines
ipc/kdbus/message.c +4 -0
@@ -178,6 +185,16 @@ int kdbus_gaps_install(struct kdbus_gaps *gaps, struct kdbus_pool_slice *slice,
        for (i = 0; i < gaps->n_memfds; ++i) {
                int memfd;
 
+               WARN_ON(!gaps->memfd_offsets[i]);
+               WARN_ON(!gaps->memfd_files[i]);
+
+               if (gaps->memfd_files[i] &&

Same as above, just drop the WARN_ON and this condition.

ipc/kdbus/message.c int kdbus_gaps_install(struct kdbus_gaps *gaps, struct kdbus_pool_slice *slice, · line 197

Quoted 6 lines
ipc/kdbus/message.c +6 -0
+                           security_file_receive(gaps->memfd_files[i])) {
+                       incomplete_fds = true;
+                       fds[n_fds++] = -1;
+                       continue;
+               }
+

I don't see the point in protecting transmission of memfds. They're treated as inline data. But I will not object it.

Patch looks good to me.

Thanks
David

Quoted 17 lines
ipc/kdbus/message.c +0 -5
                memfd = get_unused_fd_flags(O_CLOEXEC);
                if (memfd < 0) {
                        incomplete_fds = true;
@@ -193,10 +210,6 @@ int kdbus_gaps_install(struct kdbus_gaps *gaps, struct kdbus_pool_slice *slice,
                 * as usually there is no need to send more than one memfd per
                 * message.
                 */
-
-               WARN_ON(!gaps->memfd_offsets[i]);
-               WARN_ON(!gaps->memfd_files[i]);
-
                kvec.iov_base = &memfd;
                kvec.iov_len = sizeof(memfd);
                ret = kdbus_pool_slice_copy_kvec(slice, gaps->memfd_offsets[i],
--

1.9.1