dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone

Blame SOURCES/0027-SBUS-Chown-the-sbus-socket-if-needed.patch

905b4d
From 2b8bb71a6f17dd0348ae07f0488d3de76b791c7f Mon Sep 17 00:00:00 2001
905b4d
From: Jakub Hrozek <jhrozek@redhat.com>
905b4d
Date: Tue, 7 Oct 2014 11:30:01 +0200
905b4d
Subject: [PATCH 27/46] SBUS: Chown the sbus socket if needed
905b4d
905b4d
When setting up the sbus server, we might need to chown the sbus socket
905b4d
to make sure non-root peers, running as the SSSD user are able to access
905b4d
the file.
905b4d
905b4d
Reviewed-by: Pavel Reichl <preichl@redhat.com>
905b4d
Reviewed-by: Simo Sorce <simo@redhat.com>
905b4d
(cherry picked from commit 5960687483a5d3d99093c9d6ab64e11c9bde7f7b)
905b4d
---
905b4d
 src/monitor/monitor.c            |  6 +++++-
905b4d
 src/providers/data_provider_be.c |  2 +-
905b4d
 src/providers/proxy/proxy_init.c |  2 +-
905b4d
 src/sbus/sbus_client.c           | 15 +++++++++++++--
905b4d
 src/sbus/sssd_dbus.h             |  1 +
905b4d
 src/sbus/sssd_dbus_server.c      | 18 ++++++++++++++++--
905b4d
 src/tests/common_dbus.c          |  4 ++--
905b4d
 7 files changed, 39 insertions(+), 9 deletions(-)
905b4d
905b4d
diff --git a/src/monitor/monitor.c b/src/monitor/monitor.c
905b4d
index df1cd5ca14c759f7aab98094a2b8ad35731c35e6..b6777784cd289e85c865fc16490d0287a63192a5 100644
905b4d
--- a/src/monitor/monitor.c
905b4d
+++ b/src/monitor/monitor.c
905b4d
@@ -515,7 +515,11 @@ static int monitor_dbus_init(struct mt_ctx *ctx)
905b4d
         return ret;
905b4d
     }
905b4d
 
905b4d
-    ret = sbus_new_server(ctx, ctx->ev, monitor_address,
905b4d
+    /* If a service is running as unprivileged user, we need to make sure this
905b4d
+     * user can access the monitor sbus server. root is still king, so we don't
905b4d
+     * lose any access.
905b4d
+     */
905b4d
+    ret = sbus_new_server(ctx, ctx->ev, monitor_address, ctx->uid, ctx->gid,
905b4d
                           false, &ctx->sbus_srv, monitor_service_init, ctx);
905b4d
 
905b4d
     talloc_free(monitor_address);
905b4d
diff --git a/src/providers/data_provider_be.c b/src/providers/data_provider_be.c
905b4d
index 18b50214b0795709d583d5891bf4f6fd220bcb11..122c5b091751b641f815ddff5c56ac99ace69939 100644
905b4d
--- a/src/providers/data_provider_be.c
905b4d
+++ b/src/providers/data_provider_be.c
905b4d
@@ -2263,7 +2263,7 @@ static int be_srv_init(struct be_ctx *ctx)
905b4d
         return ret;
905b4d
     }
905b4d
 
905b4d
-    ret = sbus_new_server(ctx, ctx->ev, sbus_address,
905b4d
+    ret = sbus_new_server(ctx, ctx->ev, sbus_address, 0, 0,
905b4d
                           true, &ctx->sbus_srv, be_client_init, ctx);
905b4d
     if (ret != EOK) {
905b4d
         DEBUG(SSSDBG_FATAL_FAILURE, "Could not set up sbus server.\n");
905b4d
diff --git a/src/providers/proxy/proxy_init.c b/src/providers/proxy/proxy_init.c
905b4d
index dd1b75826fbfc384dd37ba659a8653547cc35bcf..1e734511766f2c0f58cffc7d726a26ecfd6c9a27 100644
905b4d
--- a/src/providers/proxy/proxy_init.c
905b4d
+++ b/src/providers/proxy/proxy_init.c
905b4d
@@ -522,7 +522,7 @@ int sssm_proxy_auth_init(struct be_ctx *bectx,
905b4d
         goto done;
905b4d
     }
905b4d
 
905b4d
-    ret = sbus_new_server(ctx, bectx->ev, sbus_address,
905b4d
+    ret = sbus_new_server(ctx, bectx->ev, sbus_address, 0, 0,
905b4d
                           false, &ctx->sbus_srv, proxy_client_init, ctx);
905b4d
     if (ret != EOK) {
905b4d
         DEBUG(SSSDBG_FATAL_FAILURE, "Could not set up sbus server.\n");
905b4d
diff --git a/src/sbus/sbus_client.c b/src/sbus/sbus_client.c
905b4d
index 6cf5002dc2b8f3b85a3110298db8255b82172ddd..8ad4c0f36a929e341793cca61fe12808e14f6bc6 100644
905b4d
--- a/src/sbus/sbus_client.c
905b4d
+++ b/src/sbus/sbus_client.c
905b4d
@@ -32,6 +32,8 @@ int sbus_client_init(TALLOC_CTX *mem_ctx,
905b4d
     struct sbus_connection *conn = NULL;
905b4d
     int ret;
905b4d
     char *filename;
905b4d
+    uid_t check_uid;
905b4d
+    gid_t check_gid;
905b4d
 
905b4d
     /* Validate input */
905b4d
     if (server_address == NULL) {
905b4d
@@ -45,8 +47,17 @@ int sbus_client_init(TALLOC_CTX *mem_ctx,
905b4d
         return EIO;
905b4d
     }
905b4d
 
905b4d
-    ret = check_file(filename,
905b4d
-                     0, 0, S_IFSOCK|S_IRUSR|S_IWUSR, 0, NULL, true);
905b4d
+    check_uid = geteuid();
905b4d
+    check_gid = getegid();
905b4d
+
905b4d
+    /* Ignore ownership checks when the server runs as root. This is the
905b4d
+     * case when privileged monitor is setting up sockets for unprivileged
905b4d
+     * responders */
905b4d
+    if (check_uid == 0) check_uid = -1;
905b4d
+    if (check_gid == 0) check_gid = -1;
905b4d
+
905b4d
+    ret = check_file(filename, check_uid, check_gid,
905b4d
+                     S_IFSOCK|S_IRUSR|S_IWUSR, 0, NULL, true);
905b4d
     if (ret != EOK) {
905b4d
         DEBUG(SSSDBG_CRIT_FAILURE, "check_file failed for [%s].\n", filename);
905b4d
         return EIO;
905b4d
diff --git a/src/sbus/sssd_dbus.h b/src/sbus/sssd_dbus.h
905b4d
index 372521a3575f967b751c9e13a7d830d9c3b43584..d01926368ce0ae5312d8ea0057a89d9a7176836b 100644
905b4d
--- a/src/sbus/sssd_dbus.h
905b4d
+++ b/src/sbus/sssd_dbus.h
905b4d
@@ -132,6 +132,7 @@ sbus_new_interface(TALLOC_CTX *mem_ctx,
905b4d
 int sbus_new_server(TALLOC_CTX *mem_ctx,
905b4d
                     struct tevent_context *ev,
905b4d
                     const char *address,
905b4d
+                    uid_t uid, gid_t gid,
905b4d
                     bool use_symlink,
905b4d
                     struct sbus_connection **server,
905b4d
                     sbus_server_conn_init_fn init_fn, void *init_pvt_data);
905b4d
diff --git a/src/sbus/sssd_dbus_server.c b/src/sbus/sssd_dbus_server.c
905b4d
index 3a7de8ff019160b2305516945740dfb6453d578b..18fb98df61f1740898f725cb0ae9924f5e2f7716 100644
905b4d
--- a/src/sbus/sssd_dbus_server.c
905b4d
+++ b/src/sbus/sssd_dbus_server.c
905b4d
@@ -181,6 +181,7 @@ remove_socket_symlink(const char *symlink_name)
905b4d
 int sbus_new_server(TALLOC_CTX *mem_ctx,
905b4d
                     struct tevent_context *ev,
905b4d
                     const char *address,
905b4d
+                    uid_t uid, gid_t gid,
905b4d
                     bool use_symlink,
905b4d
                     struct sbus_connection **_server,
905b4d
                     sbus_server_conn_init_fn init_fn,
905b4d
@@ -260,9 +261,22 @@ int sbus_new_server(TALLOC_CTX *mem_ctx,
905b4d
     if ((stat_buf.st_mode & ~S_IFMT) != (S_IRUSR|S_IWUSR)) {
905b4d
         ret = chmod(filename, (S_IRUSR|S_IWUSR));
905b4d
         if (ret != EOK) {
905b4d
+            ret = errno;
905b4d
             DEBUG(SSSDBG_CRIT_FAILURE,
905b4d
-                  "chmod failed for [%s]: [%d][%s].\n", filename, errno,
905b4d
-                                                         strerror(errno));
905b4d
+                  "chmod failed for [%s]: [%d][%s].\n", filename, ret,
905b4d
+                                                        sss_strerror(ret));
905b4d
+            ret = EIO;
905b4d
+            goto done;
905b4d
+        }
905b4d
+    }
905b4d
+
905b4d
+    if (stat_buf.st_uid != uid || stat_buf.st_gid != gid) {
905b4d
+        ret = chown(filename, uid, gid);
905b4d
+        if (ret != EOK) {
905b4d
+            ret = errno;
905b4d
+            DEBUG(SSSDBG_CRIT_FAILURE,
905b4d
+                  "chown failed for [%s]: [%d][%s].\n", filename, ret,
905b4d
+                                                        sss_strerror(ret));
905b4d
             ret = EIO;
905b4d
             goto done;
905b4d
         }
905b4d
diff --git a/src/tests/common_dbus.c b/src/tests/common_dbus.c
905b4d
index 3117c080dc3106517bee933a458583f35b04fa63..1b0ae88dc05a1514938218e97a50e9ef7b54b193 100644
905b4d
--- a/src/tests/common_dbus.c
905b4d
+++ b/src/tests/common_dbus.c
905b4d
@@ -112,8 +112,8 @@ mock_server_child(void *data)
905b4d
     ctx = talloc_new(NULL);
905b4d
     loop = tevent_context_init(ctx);
905b4d
 
905b4d
-    verify_eq (sbus_new_server(ctx, loop, mock->dbus_address, false,
905b4d
-                               &server, on_accept_connection, mock), EOK);
905b4d
+    verify_eq (sbus_new_server(ctx, loop, mock->dbus_address, geteuid(), getegid(),
905b4d
+                               false, &server, on_accept_connection, mock), EOK);
905b4d
 
905b4d
     tevent_add_fd(loop, ctx, mock->sync_fds[1], TEVENT_FD_READ,
905b4d
                   on_sync_fd_written, &stop_server);
905b4d
-- 
905b4d
1.9.3
905b4d