Skip to contentSkip to conversation

semanage - rename instead of copy

PATCH In review Joshua Brindle 2 participants 5 messages Nov 7, 2005 – Nov 8, 2005 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 semanage - rename instead of copy

Attached patch makes libsemanage copy file to a temporary file and then rename() instead of copying directly to the target. This is necessary so that an install failure doesn't leave a corrupt binary policy on the system.

Joshua

src/semanage_store.c +11 -1
diff -purN -x .svn libsemanage/src/semanage_store.c libsemanage/src/semanage_store.c
@@ -305,12 +305,18 @@ static int semanage_filename_select(cons
  * overwrite it.  Returns 0 on success, -1 on error. */
 static int semanage_copy_file(const char *src, const char *dst) {
 	int in, out, retval = 0, amount_read;
+	char tmp[PATH_MAX] = {};
 	char buf[4192];
 
 	if ((in = open(src, O_RDONLY)) == -1) {
 		return -1;
 	}
-	if ((out = open(dst, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)) == -1) {
+	
+	snprintf(tmp, PATH_MAX, "%s.tmp", dst);
+	if (!tmp)
+		return -1;
+
+	if ((out = open(tmp, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)) == -1) {
 		close(in);
 		return -1;
 	}
@@ -324,6 +330,10 @@ static int semanage_copy_file(const char
 		retval = -1;
 	close(in);
 	close(out);
+
+	if (rename(tmp, dst) == -1)
+		return -1;
+
 	return retval;
 }
 
#1 lore raw
Quoted 4 lines · Joshua Brindle

Attached patch makes libsemanage copy file to a temporary file and then rename() instead of copying directly to the target. This is necessary so that an install failure doesn't leave a corrupt binary policy on the system.

src/semanage_store.c +6 -1
diff -purN -x .svn libsemanage/src/semanage_store.c libsemanage/src/semanage_store.c
@@ -305,12 +305,18 @@ static int semanage_filename_select(cons
  * overwrite it.  Returns 0 on success, -1 on error. */
 static int semanage_copy_file(const char *src, const char *dst) {
 	int in, out, retval = 0, amount_read;
+	char tmp[PATH_MAX] = {};
 	char buf[4192];
 
 	if ((in = open(src, O_RDONLY)) == -1) {
 		return -1;
 	}
-	if ((out = open(dst, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)) == -1) {
+	
+	snprintf(tmp, PATH_MAX, "%s.tmp", dst);
+	if (!tmp)
+		return -1;
+
 

If you want to check for failure on the snprintf, you need something like:

	int n = snprintf(tmp, PATH_MAX, "%s.tmp", dst);
	if (n < 0 || n >= PATH_MAX)
		return -1;
Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#2 lore raw
Quoted 29 lines · Stephen Smalley
Quoted 4 lines · Joshua Brindle

Attached patch makes libsemanage copy file to a temporary file and then rename() instead of copying directly to the target. This is necessary so that an install failure doesn't leave a corrupt binary policy on the system.

diff -purN -x .svn libsemanage/src/semanage_store.c libsemanage/src/semanage_store.c

src/semanage_store.c +6 -1
@@ -305,12 +305,18 @@ static int semanage_filename_select(cons
  * overwrite it.  Returns 0 on success, -1 on error. */
 static int semanage_copy_file(const char *src, const char *dst) {
 	int in, out, retval = 0, amount_read;
+	char tmp[PATH_MAX] = {};
 	char buf[4192];
 
 	if ((in = open(src, O_RDONLY)) == -1) {
 		return -1;
 	}
-	if ((out = open(dst, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)) == -1) {
+	
+	snprintf(tmp, PATH_MAX, "%s.tmp", dst);
+	if (!tmp)
+		return -1;
+
 

If you want to check for failure on the snprintf, you need something like:

Quoted 4 lines
	int n = snprintf(tmp, PATH_MAX, "%s.tmp", dst);
	if (n < 0 || n >= PATH_MAX)
		return -1;

good point, also strlen(dst) can't be PATH_MAX or the filename will be truncated and you'll end up with rename(dst,dst). Do you want a new patch or are you going to fix this before commiting?

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#3 lore raw
Quoted 8 lines · Joshua Brindle
Quoted 5 lines · Stephen Smalley

If you want to check for failure on the snprintf, you need something like:

	int n = snprintf(tmp, PATH_MAX, "%s.tmp", dst);
	if (n < 0 || n >= PATH_MAX)
		return -1;

good point, also strlen(dst) can't be PATH_MAX or the filename will be truncated and you'll end up with rename(dst,dst).

That should be caught by the n >= PATH_MAX test above for the snprintf; from the man page: "If the output was truncated due to this limit then the return value is the number of characters (not including the trailing '\0') which would have been written to the final string if enough space had been available. Thus, a return value of size or more means that the output was truncated. If an output error is encountered, a negative value is returned."

Quoted 2 lines

Do you want a new
patch or are you going to fix this before commiting?

I'll make the adjustment above, and then commit it.

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#4 lore raw
Quoted 18 lines · Stephen Smalley
Quoted 8 lines · Joshua Brindle
Quoted 5 lines · Stephen Smalley

If you want to check for failure on the snprintf, you need something like:

	int n = snprintf(tmp, PATH_MAX, "%s.tmp", dst);
	if (n < 0 || n >= PATH_MAX)
		return -1;

good point, also strlen(dst) can't be PATH_MAX or the filename will be truncated and you'll end up with rename(dst,dst).

That should be caught by the n >= PATH_MAX test above for the snprintf; from the man page: "If the output was truncated due to this limit then the return value is the number of characters (not including the trailing '\0') which would have been written to the final string if enough space had been available. Thus, a return value of size or more means that the output was truncated. If an output error is encountered, a negative value is returned."

Quoted 4 lines
Quoted 2 lines

Do you want a new
patch or are you going to fix this before commiting?

I'll make the adjustment above, and then commit it.

Interdiff below. Four bug fixes: 1) corrected snprintf error checking,
2) moved snprintf prior to first open so that we don't leak a descriptor on error path, 3) added error checking for close of output file (doesn't matter for input, but could indicate truncation of output data), 4) prevent rename if there was an earlier error, since error path falls through presently for read/write errors.

Signature · 2 lines
Stephen Smalley
National Security Agency
src/semanage_store.c +9 -8
diff -u libsemanage/src/semanage_store.c libsemanage/src/semanage_store.c
@@ -304,18 +304,18 @@
 /* Copies a file from src to dst.  If dst already exists then
  * overwrite it.  Returns 0 on success, -1 on error. */
 static int semanage_copy_file(const char *src, const char *dst) {
-	int in, out, retval = 0, amount_read;
-	char tmp[PATH_MAX] = {};
+	int in, out, retval = 0, amount_read, n;
+	char tmp[PATH_MAX];
 	char buf[4192];
 
+	n = snprintf(tmp, PATH_MAX, "%s.tmp", dst);
+	if (n < 0 || n >= PATH_MAX)
+		return -1;
+
 	if ((in = open(src, O_RDONLY)) == -1) {
 		return -1;
 	}
 	
-	snprintf(tmp, PATH_MAX, "%s.tmp", dst);
-	if (!tmp)
-		return -1;
-
 	if ((out = open(tmp, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)) == -1) {
 		close(in);
 		return -1;
@@ -329,9 +329,10 @@
 	if (amount_read < 0)
 		retval = -1;
 	close(in);
-	close(out);
+	if (close(out) < 0)
+		retval = -1;
 
-	if (rename(tmp, dst) == -1)
+	if (!retval && rename(tmp, dst) == -1)
 		return -1;
 
 	return retval;