Skip to content

Commit

Permalink
smbd: NULL out "fsp" in close_file()
Browse files Browse the repository at this point in the history
Quite a few places already had this in the caller, but not all. Rename
close_file() to close_file_free() appropriately. We'll factor out
close_file_smb() doing only parts of close_file_free() later.

Signed-off-by: Volker Lendecke <[email protected]>
Reviewed-by: Jeremy Allison <[email protected]>
  • Loading branch information
vlendec authored and jrasamba committed Feb 10, 2022
1 parent 363ac75 commit f5bc73a
Show file tree
Hide file tree
Showing 16 changed files with 103 additions and 116 deletions.
20 changes: 10 additions & 10 deletions source3/lib/adouble.c
Original file line number Diff line number Diff line change
Expand Up @@ -1254,13 +1254,13 @@ static bool ad_convert_xattr(vfs_handle_struct *handle,
if (nwritten == -1) {
DBG_ERR("SMB_VFS_PWRITE failed\n");
saved_errno = errno;
close_file(NULL, fsp, ERROR_CLOSE);
close_file_free(NULL, &fsp, ERROR_CLOSE);
errno = saved_errno;
ok = false;
goto fail;
}

status = close_file(NULL, fsp, NORMAL_CLOSE);
status = close_file_free(NULL, &fsp, NORMAL_CLOSE);
if (!NT_STATUS_IS_OK(status)) {
ok = false;
goto fail;
Expand Down Expand Up @@ -1395,12 +1395,12 @@ static bool ad_convert_finderinfo(vfs_handle_struct *handle,
if (nwritten == -1) {
DBG_ERR("SMB_VFS_PWRITE failed\n");
saved_errno = errno;
close_file(NULL, fsp, ERROR_CLOSE);
close_file_free(NULL, &fsp, ERROR_CLOSE);
errno = saved_errno;
return false;
}

status = close_file(NULL, fsp, NORMAL_CLOSE);
status = close_file_free(NULL, &fsp, NORMAL_CLOSE);
if (!NT_STATUS_IS_OK(status)) {
return false;
}
Expand Down Expand Up @@ -1652,7 +1652,7 @@ static bool ad_unconvert_open_ad(TALLOC_CTX *mem_ctx,
if (ret != 0) {
DBG_ERR("SMB_VFS_FCHOWN [%s] failed: %s\n",
fsp_str_dbg(fsp), nt_errstr(status));
close_file(NULL, fsp, NORMAL_CLOSE);
close_file_free(NULL, &fsp, NORMAL_CLOSE);
return false;
}
}
Expand Down Expand Up @@ -1710,14 +1710,14 @@ static bool ad_unconvert_get_streams(struct vfs_handle_struct *handle,
num_streams,
streams);
if (!NT_STATUS_IS_OK(status)) {
close_file(NULL, fsp, NORMAL_CLOSE);
close_file_free(NULL, &fsp, NORMAL_CLOSE);
DBG_ERR("streaminfo on [%s] failed: %s\n",
smb_fname_str_dbg(smb_fname),
nt_errstr(status));
return false;
}

status = close_file(NULL, fsp, NORMAL_CLOSE);
status = close_file_free(NULL, &fsp, NORMAL_CLOSE);
if (!NT_STATUS_IS_OK(status)) {
DBG_ERR("close_file [%s] failed: %s\n",
smb_fname_str_dbg(smb_fname),
Expand Down Expand Up @@ -1975,7 +1975,7 @@ static bool ad_collect_one_stream(struct vfs_handle_struct *handle,
out:
TALLOC_FREE(sname);
if (fsp != NULL) {
status = close_file(NULL, fsp, NORMAL_CLOSE);
status = close_file_free(NULL, &fsp, NORMAL_CLOSE);
if (!NT_STATUS_IS_OK(status)) {
DBG_ERR("close_file [%s] failed: %s\n",
smb_fname_str_dbg(smb_fname),
Expand Down Expand Up @@ -2117,9 +2117,9 @@ bool ad_unconvert(TALLOC_CTX *mem_ctx,

out:
if (fsp != NULL) {
status = close_file(NULL, fsp, NORMAL_CLOSE);
status = close_file_free(NULL, &fsp, NORMAL_CLOSE);
if (!NT_STATUS_IS_OK(status)) {
DBG_ERR("close_file [%s] failed: %s\n",
DBG_ERR("close_file_free() [%s] failed: %s\n",
smb_fname_str_dbg(smb_fname),
nt_errstr(status));
ok = false;
Expand Down
12 changes: 5 additions & 7 deletions source3/modules/vfs_fruit.c
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ static bool readdir_attr_meta_finderi_stream(

fail:
if (fsp != NULL) {
close_file(NULL, fsp, NORMAL_CLOSE);
close_file_free(NULL, &fsp, NORMAL_CLOSE);
}

return ok;
Expand Down Expand Up @@ -4247,8 +4247,8 @@ static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));

if (fsp) {
close_file(req, fsp, ERROR_CLOSE);
*result = fsp = NULL;
close_file_free(req, &fsp, ERROR_CLOSE);
*result = NULL;
}

return status;
Expand Down Expand Up @@ -4993,8 +4993,7 @@ static bool fruit_get_bandsize(vfs_handle_struct *handle,

}

status = close_file(NULL, fsp, NORMAL_CLOSE);
fsp = NULL;
status = close_file_free(NULL, &fsp, NORMAL_CLOSE);
if (!NT_STATUS_IS_OK(status)) {
DBG_ERR("close_file failed: %s\n", nt_errstr(status));
ok = false;
Expand Down Expand Up @@ -5028,11 +5027,10 @@ static bool fruit_get_bandsize(vfs_handle_struct *handle,

out:
if (fsp != NULL) {
status = close_file(NULL, fsp, NORMAL_CLOSE);
status = close_file_free(NULL, &fsp, NORMAL_CLOSE);
if (!NT_STATUS_IS_OK(status)) {
DBG_ERR("close_file failed: %s\n", nt_errstr(status));
}
fsp = NULL;
}
TALLOC_FREE(plist);
TALLOC_FREE(smb_fname);
Expand Down
2 changes: 1 addition & 1 deletion source3/modules/vfs_worm.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static NTSTATUS vfs_worm_create_file(vfs_handle_struct *handle,
* Access via MAXIMUM_ALLOWED_ACCESS?
*/
if (readonly && ((*result)->access_mask & write_access_flags)) {
close_file(req, *result, NORMAL_CLOSE);
close_file_free(req, result, NORMAL_CLOSE);
return NT_STATUS_ACCESS_DENIED;
}
return NT_STATUS_OK;
Expand Down
10 changes: 4 additions & 6 deletions source3/printing/nt_printing.c
Original file line number Diff line number Diff line change
Expand Up @@ -874,8 +874,7 @@ static int file_version_is_newer(connection_struct *conn, fstring new_file, fstr
(long)old_create_time));
}

close_file(NULL, fsp, NORMAL_CLOSE);
fsp = NULL;
close_file_free(NULL, &fsp, NORMAL_CLOSE);

/* Get file version info (if available) for new file */
status = driver_unix_convert(conn, new_file, &smb_fname);
Expand Down Expand Up @@ -935,8 +934,7 @@ static int file_version_is_newer(connection_struct *conn, fstring new_file, fstr
(long)new_create_time));
}

close_file(NULL, fsp, NORMAL_CLOSE);
fsp = NULL;
close_file_free(NULL, &fsp, NORMAL_CLOSE);

if (use_version && (new_major != old_major || new_minor != old_minor)) {
/* Compare versions and choose the larger version number */
Expand Down Expand Up @@ -969,7 +967,7 @@ static int file_version_is_newer(connection_struct *conn, fstring new_file, fstr

error_exit:
if(fsp)
close_file(NULL, fsp, NORMAL_CLOSE);
close_file_free(NULL, &fsp, NORMAL_CLOSE);
ret = -1;
done:
TALLOC_FREE(smb_fname);
Expand Down Expand Up @@ -1177,7 +1175,7 @@ static uint32_t get_correct_cversion(const struct auth_session_info *session_inf
unbecome_user_without_service();
error_free_conn:
if (fsp != NULL) {
close_file(NULL, fsp, NORMAL_CLOSE);
close_file_free(NULL, &fsp, NORMAL_CLOSE);
}
if (!W_ERROR_IS_OK(*perr)) {
cversion = -1;
Expand Down
4 changes: 2 additions & 2 deletions source3/rpc_server/srvsvc/srv_srvsvc_nt.c
Original file line number Diff line number Diff line change
Expand Up @@ -2539,7 +2539,7 @@ WERROR _srvsvc_NetGetFileSecurity(struct pipes_struct *p,
error_exit:

if (fsp) {
close_file(NULL, fsp, NORMAL_CLOSE);
close_file_free(NULL, &fsp, NORMAL_CLOSE);
}

TALLOC_FREE(frame);
Expand Down Expand Up @@ -2659,7 +2659,7 @@ WERROR _srvsvc_NetSetFileSecurity(struct pipes_struct *p,
error_exit:

if (fsp) {
close_file(NULL, fsp, NORMAL_CLOSE);
close_file_free(NULL, &fsp, NORMAL_CLOSE);
}

TALLOC_FREE(frame);
Expand Down
12 changes: 8 additions & 4 deletions source3/smbd/close.c
Original file line number Diff line number Diff line change
Expand Up @@ -1478,10 +1478,12 @@ static NTSTATUS close_directory(struct smb_request *req, files_struct *fsp,
Close a files_struct.
****************************************************************************/

NTSTATUS close_file(struct smb_request *req, files_struct *fsp,
enum file_close_type close_type)
NTSTATUS close_file_free(struct smb_request *req,
struct files_struct **_fsp,
enum file_close_type close_type)
{
NTSTATUS status;
struct files_struct *fsp = *_fsp;
struct files_struct *base_fsp = fsp->base_fsp;
bool close_base_fsp = false;

Expand Down Expand Up @@ -1569,9 +1571,11 @@ NTSTATUS close_file(struct smb_request *req, files_struct *fsp,
* those loops will become confused.
*/

close_file(req, base_fsp, close_type);
close_file_free(req, &base_fsp, close_type);
}

*_fsp = NULL;

return status;
}

Expand Down Expand Up @@ -1610,5 +1614,5 @@ void msg_close_file(struct messaging_context *msg_ctx,
DEBUG(10,("msg_close_file: failed to find file.\n"));
return;
}
close_file(NULL, fsp, NORMAL_CLOSE);
close_file_free(NULL, &fsp, NORMAL_CLOSE);
}
9 changes: 6 additions & 3 deletions source3/smbd/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,12 @@ void dptr_closecnum(connection_struct *conn)
for(dptr = sconn->searches.dirptrs; dptr; dptr = next) {
next = dptr->next;
if (dptr->conn == conn) {
files_struct *fsp = dptr->dir_hnd->fsp;
close_file(NULL, fsp, NORMAL_CLOSE);
fsp = NULL;
/*
* Need to make a copy, "dptr" will be gone
* after close_file_free() returns
*/
struct files_struct *fsp = dptr->dir_hnd->fsp;
close_file_free(NULL, &fsp, NORMAL_CLOSE);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions source3/smbd/files.c
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ void file_close_conn(connection_struct *conn)
*/
fsp->op->global->durable = false;
}
close_file(NULL, fsp, SHUTDOWN_CLOSE);
close_file_free(NULL, &fsp, SHUTDOWN_CLOSE);
}
}

Expand Down Expand Up @@ -848,7 +848,7 @@ void file_close_user(struct smbd_server_connection *sconn, uint64_t vuid)
for (fsp=sconn->files; fsp; fsp=next) {
next=fsp->next;
if (fsp->vuid == vuid) {
close_file(NULL, fsp, SHUTDOWN_CLOSE);
close_file_free(NULL, &fsp, SHUTDOWN_CLOSE);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions source3/smbd/nttrans.c
Original file line number Diff line number Diff line change
Expand Up @@ -1649,7 +1649,7 @@ NTSTATUS copy_internals(TALLOC_CTX *ctx,
NULL, NULL); /* create context */

if (!NT_STATUS_IS_OK(status)) {
close_file(NULL, fsp1, ERROR_CLOSE);
close_file_free(NULL, &fsp1, ERROR_CLOSE);
goto out;
}

Expand All @@ -1663,12 +1663,12 @@ NTSTATUS copy_internals(TALLOC_CTX *ctx,
* Thus we don't look at the error return from the
* close of fsp1.
*/
close_file(NULL, fsp1, NORMAL_CLOSE);
close_file_free(NULL, &fsp1, NORMAL_CLOSE);

/* Ensure the modtime is set correctly on the destination file. */
set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);

status = close_file(NULL, fsp2, NORMAL_CLOSE);
status = close_file_free(NULL, &fsp2, NORMAL_CLOSE);

/* Grrr. We have to do this as open_file_ntcreate adds FILE_ATTRIBUTE_ARCHIVE when it
creates the file. This isn't the correct thing to do in the copy
Expand Down
13 changes: 5 additions & 8 deletions source3/smbd/open.c
Original file line number Diff line number Diff line change
Expand Up @@ -4765,7 +4765,7 @@ NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
NULL, NULL); /* create context */

if (NT_STATUS_IS_OK(status)) {
close_file(req, fsp, NORMAL_CLOSE);
close_file_free(req, &fsp, NORMAL_CLOSE);
}

return status;
Expand Down Expand Up @@ -5015,7 +5015,7 @@ static NTSTATUS open_streams_for_delete(connection_struct *conn,

DEBUG(10, ("Closing stream # %d, %s\n", j,
fsp_str_dbg(streams[j])));
close_file(NULL, streams[j], NORMAL_CLOSE);
close_file_free(NULL, &streams[j], NORMAL_CLOSE);
}

fail:
Expand Down Expand Up @@ -6058,12 +6058,10 @@ static NTSTATUS create_file_unixpath(connection_struct *conn,
* fsp->base_fsp.
*/
base_fsp = NULL;
close_file(req, fsp, ERROR_CLOSE);
fsp = NULL;
close_file_free(req, &fsp, ERROR_CLOSE);
}
if (base_fsp != NULL) {
close_file(req, base_fsp, ERROR_CLOSE);
base_fsp = NULL;
close_file_free(req, &base_fsp, ERROR_CLOSE);
}

TALLOC_FREE(parent_dir_fname);
Expand Down Expand Up @@ -6241,8 +6239,7 @@ NTSTATUS create_file_default(connection_struct *conn,
DEBUG(10, ("create_file: %s\n", nt_errstr(status)));

if (fsp != NULL) {
close_file(req, fsp, ERROR_CLOSE);
fsp = NULL;
close_file_free(req, &fsp, ERROR_CLOSE);
}
return status;
}
5 changes: 3 additions & 2 deletions source3/smbd/proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ bool smbd_smb1_brl_finish_by_mid(
/* The following definitions come from smbd/close.c */

void set_close_write_time(struct files_struct *fsp, struct timespec ts);
NTSTATUS close_file(struct smb_request *req, files_struct *fsp,
enum file_close_type close_type);
NTSTATUS close_file_free(struct smb_request *req,
struct files_struct **_fsp,
enum file_close_type close_type);
void msg_close_file(struct messaging_context *msg_ctx,
void *private_data,
uint32_t msg_type,
Expand Down
Loading

0 comments on commit f5bc73a

Please sign in to comment.