systemd: fix smack error
authorINSUN PYO <insun.pyo@samsung.com>
Fri, 2 Jul 2021 04:34:32 +0000 (13:34 +0900)
committerŁukasz Stelmach <l.stelmach@samsung.com>
Mon, 12 Feb 2024 15:37:49 +0000 (16:37 +0100)
A user systemd session accesses /proc/1/sched to detect container.

Jan 01 09:00:14 localhost audit[636]: AVC lsm=SMACK fn=smack_inode_permission action=denied subject="User" object="System::Privileged" requested=r pid=636 comm="systemd" name="sched" dev="proc" ino=12247
Jan 01 09:00:14 localhost audit[636]: AVC lsm=SMACK fn=smack_inode_permission action=denied subject="User" object="System::Privileged" requested=r pid=636 comm="systemd" name="sched" dev="proc" ino=12247
Jan 01 09:00:14 localhost audit[636]: AVC lsm=SMACK fn=smack_inode_permission action=denied subject="User" object="System::Privileged" requested=r pid=636 comm="systemd" name="sched" dev="proc" ino=12247
Jan 01 09:00:23 localhost audit[915]: AVC lsm=SMACK fn=smack_inode_permission action=denied subject="User" object="System::Privileged" requested=r pid=915 comm="systemctl" name="sched" dev="proc" ino=12247

Change-Id: I0d6f632b090582888c45f309c8a8bf06f4f0f510

src/basic/virt.c

index cdb0480..11b98fd 100644 (file)
@@ -517,20 +517,22 @@ int detect_container(void) {
         /* Interestingly /proc/1/sched actually shows the host's PID for what we see as PID 1. If the PID
          * shown there is not 1, we know we are in a PID namespace and hence a container. */
  check_sched:
-        r = read_one_line_file("/proc/1/sched", &m);
-        if (r >= 0) {
-                const char *t;
+        if (getpid_cached() == 1) {
+                r = read_one_line_file("/proc/1/sched", &m);
+                if (r >= 0) {
+                        const char *t;
 
-                t = strrchr(m, '(');
-                if (!t)
-                        return -EIO;
+                        t = strrchr(m, '(');
+                        if (!t)
+                                return -EIO;
 
-                if (!startswith(t, "(1,")) {
-                        r = VIRTUALIZATION_CONTAINER_OTHER;
-                        goto finish;
-                }
-        } else if (r != -ENOENT)
-                return r;
+                        if (!startswith(t, "(1,")) {
+                                r = VIRTUALIZATION_CONTAINER_OTHER;
+                                goto finish;
+                        }
+                } else if (r != -ENOENT)
+                        return r;
+        }
 
         /* If that didn't work, give up, assume no container manager. */
         r = VIRTUALIZATION_NONE;