[PATCH v2 02/10] sandbox/seunshare: make a best-effort to remove tmpdir on error paths

Stephen Smalley <[email protected]>
Newsgroups org.kernel.vger.selinux
Message-ID <[email protected]>
Try to remove the tmpdir on error paths. Failure is acceptable.

Signed-off-by: Stephen Smalley <[email protected]>
---
 sandbox/seunshare.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/sandbox/seunshare.c b/sandbox/seunshare.c
index 0c32d03f..d0f3ef1f 100644
--- a/sandbox/seunshare.c
+++ b/sandbox/seunshare.c
@@ -663,6 +663,7 @@ static char *create_tmpdir(const char *src, struct stat *src_st,
 	int fd_t = -1, fd_s = -1;
 	struct stat tmp_st;
 	char *con = NULL;
+	bool created = false;
 
 	/* get selinux context of source directory */
 	if (execcon) {
@@ -698,6 +699,8 @@ static char *create_tmpdir(const char *src, struct stat *src_st,
 		goto err;
 	}
 
+	created = true;
+
 	/* temporary directory must be owned by root:user */
 	fd_t = pin_dir(tmpdir, out_st);
 	if (fd_t < 0)
@@ -747,11 +750,22 @@ static char *create_tmpdir(const char *src, struct stat *src_st,
 		fprintf(stderr,
 			_("Failed to populate runtime temporary directory\n"));
 		cleanup_tmpdir(tmpdir, src, pwd, 0);
+		created = false;
 		goto err;
 	}
 
 	goto good;
 err:
+	if (created) {
+		/*
+		 * Best-effort removal of still-empty tmpdir.
+		 * We do not use setfsuid_checked() here
+		 * because the fsuid is indeterminate on
+		 * some error paths.
+		 */
+		(void)setfsuid(0);
+		(void)rmdir(tmpdir);
+	}
 	free(tmpdir);
 	tmpdir = NULL;
 good:
-- 
2.55.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.