Commit 70fe7dc0 authored by Jeff Layton's avatar Jeff Layton Committed by Steve French

[CIFS] clean up error handling in cifs_mount

Move all of the kfree's sprinkled in the middle of the function to the
end, and have the code set rc and just goto there on error. Also zero
out the password string before freeing it. Looks like this should also
fix a potential memory leak of the prepath string if an error occurs
near the end of the function.
Signed-off-by: default avatarJeff Layton <jlayton@redhat.com>
Signed-off-by: default avatarSteve French <sfrench@us.ibm.com>
parent 68bf728a
...@@ -1781,11 +1781,8 @@ cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb, ...@@ -1781,11 +1781,8 @@ cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb,
memset(&volume_info, 0, sizeof(struct smb_vol)); memset(&volume_info, 0, sizeof(struct smb_vol));
if (cifs_parse_mount_options(mount_data, devname, &volume_info)) { if (cifs_parse_mount_options(mount_data, devname, &volume_info)) {
kfree(volume_info.UNC); rc = -EINVAL;
kfree(volume_info.password); goto out;
kfree(volume_info.prepath);
FreeXid(xid);
return -EINVAL;
} }
if (volume_info.nullauth) { if (volume_info.nullauth) {
...@@ -1798,11 +1795,8 @@ cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb, ...@@ -1798,11 +1795,8 @@ cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb,
cifserror("No username specified"); cifserror("No username specified");
/* In userspace mount helper we can get user name from alternate /* In userspace mount helper we can get user name from alternate
locations such as env variables and files on disk */ locations such as env variables and files on disk */
kfree(volume_info.UNC); rc = -EINVAL;
kfree(volume_info.password); goto out;
kfree(volume_info.prepath);
FreeXid(xid);
return -EINVAL;
} }
if (volume_info.UNCip && volume_info.UNC) { if (volume_info.UNCip && volume_info.UNC) {
...@@ -1821,11 +1815,8 @@ cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb, ...@@ -1821,11 +1815,8 @@ cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb,
if (rc <= 0) { if (rc <= 0) {
/* we failed translating address */ /* we failed translating address */
kfree(volume_info.UNC); rc = -EINVAL;
kfree(volume_info.password); goto out;
kfree(volume_info.prepath);
FreeXid(xid);
return -EINVAL;
} }
cFYI(1, ("UNC: %s ip: %s", volume_info.UNC, volume_info.UNCip)); cFYI(1, ("UNC: %s ip: %s", volume_info.UNC, volume_info.UNCip));
...@@ -1835,20 +1826,14 @@ cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb, ...@@ -1835,20 +1826,14 @@ cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb,
/* BB using ip addr as server name to connect to the /* BB using ip addr as server name to connect to the
DFS root below */ DFS root below */
cERROR(1, ("Connecting to DFS root not implemented yet")); cERROR(1, ("Connecting to DFS root not implemented yet"));
kfree(volume_info.UNC); rc = -EINVAL;
kfree(volume_info.password); goto out;
kfree(volume_info.prepath);
FreeXid(xid);
return -EINVAL;
} else /* which servers DFS root would we conect to */ { } else /* which servers DFS root would we conect to */ {
cERROR(1, cERROR(1,
("CIFS mount error: No UNC path (e.g. -o " ("CIFS mount error: No UNC path (e.g. -o "
"unc=//192.168.1.100/public) specified")); "unc=//192.168.1.100/public) specified"));
kfree(volume_info.UNC); rc = -EINVAL;
kfree(volume_info.password); goto out;
kfree(volume_info.prepath);
FreeXid(xid);
return -EINVAL;
} }
/* this is needed for ASCII cp to Unicode converts */ /* this is needed for ASCII cp to Unicode converts */
...@@ -1860,11 +1845,8 @@ cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb, ...@@ -1860,11 +1845,8 @@ cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb,
if (cifs_sb->local_nls == NULL) { if (cifs_sb->local_nls == NULL) {
cERROR(1, ("CIFS mount error: iocharset %s not found", cERROR(1, ("CIFS mount error: iocharset %s not found",
volume_info.iocharset)); volume_info.iocharset));
kfree(volume_info.UNC); rc = -ELIBACC;
kfree(volume_info.password); goto out;
kfree(volume_info.prepath);
FreeXid(xid);
return -ELIBACC;
} }
} }
...@@ -1878,11 +1860,8 @@ cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb, ...@@ -1878,11 +1860,8 @@ cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb,
&sin_server6.sin6_addr, &sin_server6.sin6_addr,
volume_info.username, &srvTcp); volume_info.username, &srvTcp);
} else { } else {
kfree(volume_info.UNC); rc = -EINVAL;
kfree(volume_info.password); goto out;
kfree(volume_info.prepath);
FreeXid(xid);
return -EINVAL;
} }
if (srvTcp) { if (srvTcp) {
...@@ -1906,22 +1885,14 @@ cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb, ...@@ -1906,22 +1885,14 @@ cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb,
"Aborting operation")); "Aborting operation"));
if (csocket != NULL) if (csocket != NULL)
sock_release(csocket); sock_release(csocket);
kfree(volume_info.UNC); goto out;
kfree(volume_info.password);
kfree(volume_info.prepath);
FreeXid(xid);
return rc;
} }
srvTcp = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL); srvTcp = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
if (!srvTcp) { if (!srvTcp) {
rc = -ENOMEM; rc = -ENOMEM;
sock_release(csocket); sock_release(csocket);
kfree(volume_info.UNC); goto out;
kfree(volume_info.password);
kfree(volume_info.prepath);
FreeXid(xid);
return rc;
} else { } else {
memcpy(&srvTcp->addr.sockAddr, &sin_server, memcpy(&srvTcp->addr.sockAddr, &sin_server,
sizeof(struct sockaddr_in)); sizeof(struct sockaddr_in));
...@@ -1943,11 +1914,7 @@ cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb, ...@@ -1943,11 +1914,7 @@ cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb,
cERROR(1, ("error %d create cifsd thread", rc)); cERROR(1, ("error %d create cifsd thread", rc));
srvTcp->tsk = NULL; srvTcp->tsk = NULL;
sock_release(csocket); sock_release(csocket);
kfree(volume_info.UNC); goto out;
kfree(volume_info.password);
kfree(volume_info.prepath);
FreeXid(xid);
return rc;
} }
wait_for_completion(&cifsd_complete); wait_for_completion(&cifsd_complete);
rc = 0; rc = 0;
...@@ -1962,8 +1929,6 @@ cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb, ...@@ -1962,8 +1929,6 @@ cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb,
if (existingCifsSes) { if (existingCifsSes) {
pSesInfo = existingCifsSes; pSesInfo = existingCifsSes;
cFYI(1, ("Existing smb sess found")); cFYI(1, ("Existing smb sess found"));
kfree(volume_info.password);
/* volume_info.UNC freed at end of function */
} else if (!rc) { } else if (!rc) {
cFYI(1, ("Existing smb sess not found")); cFYI(1, ("Existing smb sess not found"));
pSesInfo = sesInfoAlloc(); pSesInfo = sesInfoAlloc();
...@@ -1977,8 +1942,11 @@ cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb, ...@@ -1977,8 +1942,11 @@ cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb,
if (!rc) { if (!rc) {
/* volume_info.password freed at unmount */ /* volume_info.password freed at unmount */
if (volume_info.password) if (volume_info.password) {
pSesInfo->password = volume_info.password; pSesInfo->password = volume_info.password;
/* set to NULL to prevent freeing on exit */
volume_info.password = NULL;
}
if (volume_info.username) if (volume_info.username)
strncpy(pSesInfo->userName, strncpy(pSesInfo->userName,
volume_info.username, volume_info.username,
...@@ -2000,8 +1968,7 @@ cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb, ...@@ -2000,8 +1968,7 @@ cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb,
up(&pSesInfo->sesSem); up(&pSesInfo->sesSem);
if (!rc) if (!rc)
atomic_inc(&srvTcp->socketUseCount); atomic_inc(&srvTcp->socketUseCount);
} else }
kfree(volume_info.password);
} }
/* search for existing tcon to this server share */ /* search for existing tcon to this server share */
...@@ -2106,9 +2073,8 @@ cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb, ...@@ -2106,9 +2073,8 @@ cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb,
"", cifs_sb->local_nls, "", cifs_sb->local_nls,
cifs_sb->mnt_cifs_flags & cifs_sb->mnt_cifs_flags &
CIFS_MOUNT_MAP_SPECIAL_CHR); CIFS_MOUNT_MAP_SPECIAL_CHR);
kfree(volume_info.UNC); rc = -ENODEV;
FreeXid(xid); goto out;
return -ENODEV;
} else { } else {
/* BB Do we need to wrap sesSem around /* BB Do we need to wrap sesSem around
* this TCon call and Unix SetFS as * this TCon call and Unix SetFS as
...@@ -2231,6 +2197,12 @@ cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb, ...@@ -2231,6 +2197,12 @@ cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb,
(in which case it is not needed anymore) but when new sesion is created (in which case it is not needed anymore) but when new sesion is created
the password ptr is put in the new session structure (in which case the the password ptr is put in the new session structure (in which case the
password will be freed at unmount time) */ password will be freed at unmount time) */
out:
/* zero out password before freeing */
if (volume_info.password != NULL) {
memset(volume_info.password, 0, strlen(volume_info.password));
kfree(volume_info.password);
}
kfree(volume_info.UNC); kfree(volume_info.UNC);
kfree(volume_info.prepath); kfree(volume_info.prepath);
FreeXid(xid); FreeXid(xid);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment