Skip to contentSkip to conversation

libsemanage/src/semanage_store.c:semanage_split_fc

PATCH Serge E. Hallyn 3 participants 4 messages Mar 6, 2006 – Mar 8, 2006 SELinux security module

lore
Share & tools
lore
#0 lore raw

In semanage_split_fc(), linex 887 and 893, the file_con fd is not closed on error. Trivial patch follows.

-serge

src/semanage_store.c +2 -0
Index: libsemanage/src/semanage_store.c
===================================================================
@@ -885,11 +885,13 @@ int semanage_split_fc(semanage_handle_t 
 	fc = open(semanage_path(SEMANAGE_TMP, SEMANAGE_FC), O_WRONLY | O_CREAT | O_TRUNC,  S_IRUSR | S_IWUSR);
 	if (!fc) {
 		ERR(sh, "Could not open %s for writing.", semanage_path(SEMANAGE_TMP, SEMANAGE_FC));
+		fclose(file_con);
 		return -1;
 	}
 	hd = open(semanage_path(SEMANAGE_TMP, SEMANAGE_HOMEDIR_TMPL), O_WRONLY | O_CREAT | O_TRUNC,  S_IRUSR | S_IWUSR);
 	if (!hd) {
 		ERR(sh, "Could not open %s for writing.", semanage_path(SEMANAGE_TMP, SEMANAGE_HOMEDIR_TMPL));
+		fclose(file_con);
 		close(fc);
 		return -1;
 	}

--
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.

#1 lore raw
Quoted 4 lines · Serge E. Hallyn

In semanage_split_fc(), linex 887 and 893, the file_con fd is not closed on error. Trivial patch follows.

There's already a cleanup: goto target, I think that should be used instead (after initializing the file descriptors, and making the close statements conditional).

It'd be nice if close(-1) and fclose(NULL) were no-ops.

--
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 10 lines · Ivan Gyurdiev
Quoted 4 lines · Serge E. Hallyn

In semanage_split_fc(), linex 887 and 893, the file_con fd is not closed on error. Trivial patch follows.

There's already a cleanup: goto target, I think that should be used instead (after initializing the file descriptors, and making the close statements conditional).

It'd be nice if close(-1) and fclose(NULL) were no-ops.

Like this?

Index: libsemanage/src/semanage_store.c
===================================================================
RCS file: /nfshome/pal/CVS/selinux-usr/libsemanage/src/semanage_store.c,v retrieving revision 1.43

src/semanage_store.c +13 -12
diff -u -p -r1.43 semanage_store.c
@@ -871,27 +871,26 @@ hidden_def(semanage_reload_policy)
 
 /* This expands the file_context.tmpl file to file_context and homedirs.template */
 int semanage_split_fc(semanage_handle_t *sh) {
-	FILE *file_con;
-	int fc, hd, retval = 0;
+	FILE *file_con = NULL;
+	int fc = -1, hd = -1, retval = -1;
 	char buf[PATH_MAX] = {0};
 
 	/* I use fopen here instead of open so that I can use fgets which only reads a single line */
 	file_con = fopen(semanage_path(SEMANAGE_TMP, SEMANAGE_FC_TMPL), "r");
 	if (!file_con) {
 		ERR(sh, "Could not open %s for reading.", semanage_path(SEMANAGE_TMP, SEMANAGE_FC_TMPL));
-		return -1;
+		goto cleanup;
 	}
 
 	fc = open(semanage_path(SEMANAGE_TMP, SEMANAGE_FC), O_WRONLY | O_CREAT | O_TRUNC,  S_IRUSR | S_IWUSR);
 	if (!fc) {
 		ERR(sh, "Could not open %s for writing.", semanage_path(SEMANAGE_TMP, SEMANAGE_FC));
-		return -1;
+		goto cleanup;
 	}
 	hd = open(semanage_path(SEMANAGE_TMP, SEMANAGE_HOMEDIR_TMPL), O_WRONLY | O_CREAT | O_TRUNC,  S_IRUSR | S_IWUSR);
 	if (!hd) {
 		ERR(sh, "Could not open %s for writing.", semanage_path(SEMANAGE_TMP, SEMANAGE_HOMEDIR_TMPL));
-		close(fc);
-		return -1;
+		goto cleanup;
 	}
 
 	while (fgets_unlocked(buf, PATH_MAX, file_con)) {
@@ -901,22 +900,24 @@ int semanage_split_fc(semanage_handle_t 
 			/* This contains one of the template variables, write it to homedir.template */
 			if (write(hd, buf, strnlen(buf, PATH_MAX)) == 0) {
 				ERR(sh, "Write to %s failed.", semanage_path(SEMANAGE_TMP, SEMANAGE_HOMEDIR_TMPL));
-				retval = -1;
 				goto cleanup;
 			}
 		} else {
 			if (write(fc, buf, strnlen(buf, PATH_MAX)) == 0) {
 				ERR(sh, "Write to %s failed.", semanage_path(SEMANAGE_TMP, SEMANAGE_FC));
-				retval = -1;	
 				goto cleanup;
 			}
 		}
 	}
-	
+
+	retval = 0;
 cleanup:
-	fclose(file_con);
-	close(fc);
-	close(hd);	
+	if (file_con)
+		fclose(file_con);
+	if (fc >= 0)
+		close(fc);
+	if (hd >= 0)
+		close(hd);
 
 	return retval;
 		
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.
#3 lore raw
Quoted 6 lines · Stephen Smalley
Quoted 5 lines · Ivan Gyurdiev
Quoted 4 lines · Serge E. Hallyn

In semanage_split_fc(), linex 887 and 893, the file_con fd is not closed on error. Trivial patch follows.

Quoted 7 lines
Quoted 5 lines

There's already a cleanup: goto target, I think that should be used instead (after initializing the file descriptors, and making the close statements conditional).

It'd be nice if close(-1) and fclose(NULL) were no-ops.

Like this?

Agreed, looks far more maintainable.

thanks,
-serge

--
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.