52b84b
From af20a66874296f71618819ebce9d4335b195728c Mon Sep 17 00:00:00 2001
52b84b
From: Lennart Poettering <lennart@poettering.net>
52b84b
Date: Wed, 22 Jan 2020 12:04:38 +0100
52b84b
Subject: [PATCH] logind: check PolicyKit before allowing VT switch
52b84b
52b84b
Let's lock this down a bit. Effectively nothing much changes, since the
52b84b
default PK policy will allow users on the VT to change VT. Only users
52b84b
with no local VT session won't be able to switch VTs.
52b84b
52b84b
(cherry picked from commit 4acf0cfd2f92edb94ad48d04f1ce6c9ab4e19d55)
52b84b
52b84b
Resolves: #1797679
52b84b
---
52b84b
 src/login/logind-dbus.c                 | 16 +++++++
52b84b
 src/login/logind-seat-dbus.c            | 58 ++++++++++++++++++++++++-
52b84b
 src/login/logind-session-dbus.c         | 15 +++++++
52b84b
 src/login/org.freedesktop.login1.policy | 10 +++++
52b84b
 4 files changed, 98 insertions(+), 1 deletion(-)
52b84b
52b84b
diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c
52b84b
index dca7f4a30f..3f122fcbd9 100644
52b84b
--- a/src/login/logind-dbus.c
52b84b
+++ b/src/login/logind-dbus.c
52b84b
@@ -913,6 +913,8 @@ static int method_activate_session(sd_bus_message *message, void *userdata, sd_b
52b84b
         if (r < 0)
52b84b
                 return r;
52b84b
 
52b84b
+        /* PolicyKit is done by bus_session_method_activate() */
52b84b
+
52b84b
         return bus_session_method_activate(message, session, error);
52b84b
 }
52b84b
 
52b84b
@@ -944,6 +946,20 @@ static int method_activate_session_on_seat(sd_bus_message *message, void *userda
52b84b
         if (session->seat != seat)
52b84b
                 return sd_bus_error_setf(error, BUS_ERROR_SESSION_NOT_ON_SEAT, "Session %s not on seat %s", session_name, seat_name);
52b84b
 
52b84b
+        r = bus_verify_polkit_async(
52b84b
+                        message,
52b84b
+                        CAP_SYS_ADMIN,
52b84b
+                        "org.freedesktop.login1.chvt",
52b84b
+                        NULL,
52b84b
+                        false,
52b84b
+                        UID_INVALID,
52b84b
+                        &m->polkit_registry,
52b84b
+                        error);
52b84b
+        if (r < 0)
52b84b
+                return r;
52b84b
+        if (r == 0)
52b84b
+                return 1; /* Will call us back */
52b84b
+
52b84b
         r = session_activate(session);
52b84b
         if (r < 0)
52b84b
                 return r;
52b84b
diff --git a/src/login/logind-seat-dbus.c b/src/login/logind-seat-dbus.c
52b84b
index c4d9b067c6..2e590a8f21 100644
52b84b
--- a/src/login/logind-seat-dbus.c
52b84b
+++ b/src/login/logind-seat-dbus.c
52b84b
@@ -174,6 +174,20 @@ static int method_activate_session(sd_bus_message *message, void *userdata, sd_b
52b84b
         if (session->seat != s)
52b84b
                 return sd_bus_error_setf(error, BUS_ERROR_SESSION_NOT_ON_SEAT, "Session %s not on seat %s", name, s->id);
52b84b
 
52b84b
+        r = bus_verify_polkit_async(
52b84b
+                        message,
52b84b
+                        CAP_SYS_ADMIN,
52b84b
+                        "org.freedesktop.login1.chvt",
52b84b
+                        NULL,
52b84b
+                        false,
52b84b
+                        UID_INVALID,
52b84b
+                        &s->manager->polkit_registry,
52b84b
+                        error);
52b84b
+        if (r < 0)
52b84b
+                return r;
52b84b
+        if (r == 0)
52b84b
+                return 1; /* Will call us back */
52b84b
+
52b84b
         r = session_activate(session);
52b84b
         if (r < 0)
52b84b
                 return r;
52b84b
@@ -194,7 +208,21 @@ static int method_switch_to(sd_bus_message *message, void *userdata, sd_bus_erro
52b84b
                 return r;
52b84b
 
52b84b
         if (to <= 0)
52b84b
-                return -EINVAL;
52b84b
+                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid virtual terminal");
52b84b
+
52b84b
+        r = bus_verify_polkit_async(
52b84b
+                        message,
52b84b
+                        CAP_SYS_ADMIN,
52b84b
+                        "org.freedesktop.login1.chvt",
52b84b
+                        NULL,
52b84b
+                        false,
52b84b
+                        UID_INVALID,
52b84b
+                        &s->manager->polkit_registry,
52b84b
+                        error);
52b84b
+        if (r < 0)
52b84b
+                return r;
52b84b
+        if (r == 0)
52b84b
+                return 1; /* Will call us back */
52b84b
 
52b84b
         r = seat_switch_to(s, to);
52b84b
         if (r < 0)
52b84b
@@ -210,6 +238,20 @@ static int method_switch_to_next(sd_bus_message *message, void *userdata, sd_bus
52b84b
         assert(message);
52b84b
         assert(s);
52b84b
 
52b84b
+        r = bus_verify_polkit_async(
52b84b
+                        message,
52b84b
+                        CAP_SYS_ADMIN,
52b84b
+                        "org.freedesktop.login1.chvt",
52b84b
+                        NULL,
52b84b
+                        false,
52b84b
+                        UID_INVALID,
52b84b
+                        &s->manager->polkit_registry,
52b84b
+                        error);
52b84b
+        if (r < 0)
52b84b
+                return r;
52b84b
+        if (r == 0)
52b84b
+                return 1; /* Will call us back */
52b84b
+
52b84b
         r = seat_switch_to_next(s);
52b84b
         if (r < 0)
52b84b
                 return r;
52b84b
@@ -224,6 +266,20 @@ static int method_switch_to_previous(sd_bus_message *message, void *userdata, sd
52b84b
         assert(message);
52b84b
         assert(s);
52b84b
 
52b84b
+        r = bus_verify_polkit_async(
52b84b
+                        message,
52b84b
+                        CAP_SYS_ADMIN,
52b84b
+                        "org.freedesktop.login1.chvt",
52b84b
+                        NULL,
52b84b
+                        false,
52b84b
+                        UID_INVALID,
52b84b
+                        &s->manager->polkit_registry,
52b84b
+                        error);
52b84b
+        if (r < 0)
52b84b
+                return r;
52b84b
+        if (r == 0)
52b84b
+                return 1; /* Will call us back */
52b84b
+
52b84b
         r = seat_switch_to_previous(s);
52b84b
         if (r < 0)
52b84b
                 return r;
52b84b
diff --git a/src/login/logind-session-dbus.c b/src/login/logind-session-dbus.c
52b84b
index 25c4981dc0..88a2d33dc8 100644
52b84b
--- a/src/login/logind-session-dbus.c
52b84b
+++ b/src/login/logind-session-dbus.c
52b84b
@@ -13,6 +13,7 @@
52b84b
 #include "logind.h"
52b84b
 #include "signal-util.h"
52b84b
 #include "strv.h"
52b84b
+#include "user-util.h"
52b84b
 #include "util.h"
52b84b
 
52b84b
 static int property_get_user(
52b84b
@@ -182,6 +183,20 @@ int bus_session_method_activate(sd_bus_message *message, void *userdata, sd_bus_
52b84b
         assert(message);
52b84b
         assert(s);
52b84b
 
52b84b
+        r = bus_verify_polkit_async(
52b84b
+                        message,
52b84b
+                        CAP_SYS_ADMIN,
52b84b
+                        "org.freedesktop.login1.chvt",
52b84b
+                        NULL,
52b84b
+                        false,
52b84b
+                        UID_INVALID,
52b84b
+                        &s->manager->polkit_registry,
52b84b
+                        error);
52b84b
+        if (r < 0)
52b84b
+                return r;
52b84b
+        if (r == 0)
52b84b
+                return 1; /* Will call us back */
52b84b
+
52b84b
         r = session_activate(s);
52b84b
         if (r < 0)
52b84b
                 return r;
52b84b
diff --git a/src/login/org.freedesktop.login1.policy b/src/login/org.freedesktop.login1.policy
52b84b
index f1d1f956d3..83760e1580 100644
52b84b
--- a/src/login/org.freedesktop.login1.policy
52b84b
+++ b/src/login/org.freedesktop.login1.policy
52b84b
@@ -357,4 +357,14 @@
52b84b
                 </defaults>
52b84b
         </action>
52b84b
 
52b84b
+        <action id="org.freedesktop.login1.chvt">
52b84b
+                <description gettext-domain="systemd">Change Session</description>
52b84b
+                <message gettext-domain="systemd">Authentication is required for changing the virtual terminal.</message>
52b84b
+                <defaults>
52b84b
+                        <allow_any>auth_admin_keep</allow_any>
52b84b
+                        <allow_inactive>auth_admin_keep</allow_inactive>
52b84b
+                        <allow_active>yes</allow_active>
52b84b
+                </defaults>
52b84b
+        </action>
52b84b
+
52b84b
 </policyconfig>