dcavalca / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
Michal Sekletar 9bf975
From c96e341a0d317d6345dbf1f43a8ca0e0c31aaf7d Mon Sep 17 00:00:00 2001
Michal Sekletar 9bf975
From: Djalal Harouni <tixxdz@opendz.org>
Michal Sekletar 9bf975
Date: Thu, 17 Apr 2014 01:47:11 +0100
Michal Sekletar 9bf975
Subject: [PATCH 387/389] install: create_symlink() check unlink() return value
Michal Sekletar 9bf975
Michal Sekletar 9bf975
create_symlink() do not check the return value of unlink(), this may
Michal Sekletar 9bf975
confuse the user.
Michal Sekletar 9bf975
Michal Sekletar 9bf975
Before the unlink() call we check the 'force' argument. If it is not set
Michal Sekletar 9bf975
we fail with -EEXIST, otherwise we unlink() the file, therefore the next
Michal Sekletar 9bf975
symlink() should not fail with -EEXIST (do not count races...).
Michal Sekletar 9bf975
Michal Sekletar 9bf975
However since callers may not have appropriate privileges to unlink()
Michal Sekletar 9bf975
the file we lose the -EPERM or any other errno code of unlink(), and
Michal Sekletar 9bf975
return the -EEXIST of the next symlink(). Fix this by checking unlink()
Michal Sekletar 9bf975
results.
Michal Sekletar 9bf975
Michal Sekletar 9bf975
Before:
Michal Sekletar 9bf975
$ systemctl --force --root=~/container-03 set-default multi-user.target
Michal Sekletar 9bf975
Failed to set default target: File exists
Michal Sekletar 9bf975
Michal Sekletar 9bf975
After:
Michal Sekletar 9bf975
$ systemctl --force --root=~/container-03 set-default multi-user.target
Michal Sekletar 9bf975
Failed to set default target: Permission denied
Michal Sekletar 9bf975
Michal Sekletar 9bf975
(cherry picked from commit af7fce1cdb6c0d6ce56bcddccbc31dd3d64a8cd8)
Michal Sekletar 9bf975
(cherry picked from commit ac19aafa62b4dd47e26b368b54c17e6e1c71d8fb)
Michal Sekletar 9bf975
---
Michal Sekletar 9bf975
 src/shared/install.c | 4 +++-
Michal Sekletar 9bf975
 1 file changed, 3 insertions(+), 1 deletion(-)
Michal Sekletar 9bf975
Michal Sekletar 9bf975
diff --git a/src/shared/install.c b/src/shared/install.c
Michal Sekletar 9bf975
index b9c85b7..e6a61fa 100644
Michal Sekletar 9bf975
--- a/src/shared/install.c
Michal Sekletar 9bf975
+++ b/src/shared/install.c
Michal Sekletar 9bf975
@@ -1172,7 +1172,9 @@ static int create_symlink(
Michal Sekletar 9bf975
         if (!force)
Michal Sekletar 9bf975
                 return -EEXIST;
Michal Sekletar 9bf975
 
Michal Sekletar 9bf975
-        unlink(new_path);
Michal Sekletar 9bf975
+        r = unlink(new_path);
Michal Sekletar 9bf975
+        if (r < 0 && errno != ENOENT)
Michal Sekletar 9bf975
+                return -errno;
Michal Sekletar 9bf975
 
Michal Sekletar 9bf975
         if (symlink(old_path, new_path) >= 0) {
Michal Sekletar 9bf975
                 add_file_change(changes, n_changes, UNIT_FILE_UNLINK, new_path, NULL);
Michal Sekletar 9bf975
-- 
Michal Sekletar 9bf975
1.8.3.1
Michal Sekletar 9bf975