summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Schneider <asn@samba.org>2018-08-09 16:42:43 +0200
committerJeremy Allison <jra@samba.org>2018-08-11 01:49:16 +0200
commitd4fb124adfc10de8b7eb1f72b74d7ca83f8415dd (patch)
treebdc9b422694213aa8d18f8770f7e43f1244cd97b
parentb7b4fc51d0eadbbc94576dda75ae80098a205a24 (diff)
downloadsamba-d4fb124adfc10de8b7eb1f72b74d7ca83f8415dd.tar.gz
s4:lib: Fix a possible fd leak in gp_get_file()
Found by covscan. BUG: https://bugzilla.samba.org/show_bug.cgi?id=13567 Pair-Programmed-With: Justin Stephenson <jstephen@redhat.com> Signed-off-by: Andreas Schneider <asn@samba.org> Signed-off-by: Justin Stephenson <jstephen@redhat.com> Reviewed-by: Jeremy Allison <jra@samba.org>
-rw-r--r--source4/lib/policy/gp_filesys.c24
1 files changed, 16 insertions, 8 deletions
diff --git a/source4/lib/policy/gp_filesys.c b/source4/lib/policy/gp_filesys.c
index d48fc9fd6b0..267762dd27d 100644
--- a/source4/lib/policy/gp_filesys.c
+++ b/source4/lib/policy/gp_filesys.c
@@ -215,6 +215,7 @@ static NTSTATUS gp_get_file (struct smbcli_tree *tree, const char *remote_src,
fh_local = open(local_dst, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fh_local == -1) {
DEBUG(0, ("Failed to open local file: %s\n", local_dst));
+ smbcli_close(tree, fh_remote);
return NT_STATUS_UNSUCCESSFUL;
}
@@ -224,11 +225,17 @@ static NTSTATUS gp_get_file (struct smbcli_tree *tree, const char *remote_src,
NT_STATUS_IS_ERR(smbcli_getattrE(tree, fh_remote,
&attr, &file_size, NULL, NULL, NULL))) {
DEBUG(0, ("Failed to get remote file size: %s\n", smbcli_errstr(tree)));
+ smbcli_close(tree, fh_remote);
+ close(fh_local);
return NT_STATUS_UNSUCCESSFUL;
}
buf = talloc_zero_array(tree, uint8_t, buf_size);
- NT_STATUS_HAVE_NO_MEMORY(buf);
+ if (buf == NULL) {
+ smbcli_close(tree, fh_remote);
+ close(fh_local);
+ return NT_STATUS_NO_MEMORY;
+ }
/* Copy the contents of the file */
while (1) {
@@ -240,27 +247,28 @@ static NTSTATUS gp_get_file (struct smbcli_tree *tree, const char *remote_src,
if (write(fh_local, buf, n) != n) {
DEBUG(0, ("Short write while copying file.\n"));
+ smbcli_close(tree, fh_remote);
+ close(fh_local);
talloc_free(buf);
return NT_STATUS_UNSUCCESSFUL;
}
nread += n;
}
+ /* Close the files */
+ smbcli_close(tree, fh_remote);
+ close(fh_local);
+
+ talloc_free(buf);
+
/* Bytes read should match the file size, or the copy was incomplete */
if (nread != file_size) {
DEBUG(0, ("Remote/local file size mismatch after copying file: "
"%s (remote %zu, local %zu).\n",
remote_src, file_size, nread));
- close(fh_local);
- talloc_free(buf);
return NT_STATUS_UNSUCCESSFUL;
}
- /* Close the files */
- smbcli_close(tree, fh_remote);
- close(fh_local);
-
- talloc_free(buf);
return NT_STATUS_OK;
}