Commit 8d767223 authored by Al Viro's avatar Al Viro Committed by Steve French

cifs: don't cargo-cult strndup()

strndup(s, strlen(s)) is a highly unidiomatic way to spell strdup(s);
it's *NOT* safer in any way, since strlen() is just as sensitive to
NUL-termination as strdup() is.

strndup() is for situations when you need a copy of a known-sized
substring, not a magic security juju to drive the bad spirits away.
Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
Signed-off-by: default avatarSteve French <stfrench@microsoft.com>
parent b9335f62
...@@ -270,7 +270,7 @@ static struct vfsmount *cifs_dfs_do_mount(struct dentry *mntpt, ...@@ -270,7 +270,7 @@ static struct vfsmount *cifs_dfs_do_mount(struct dentry *mntpt,
char *mountdata; char *mountdata;
char *devname; char *devname;
devname = kstrndup(fullpath, strlen(fullpath), GFP_KERNEL); devname = kstrdup(fullpath, GFP_KERNEL);
if (!devname) if (!devname)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
......
...@@ -1778,9 +1778,7 @@ cifs_set_cifscreds(struct smb3_fs_context *ctx, struct cifs_ses *ses) ...@@ -1778,9 +1778,7 @@ cifs_set_cifscreds(struct smb3_fs_context *ctx, struct cifs_ses *ses)
* for the request. * for the request.
*/ */
if (is_domain && ses->domainName) { if (is_domain && ses->domainName) {
ctx->domainname = kstrndup(ses->domainName, ctx->domainname = kstrdup(ses->domainName, GFP_KERNEL);
strlen(ses->domainName),
GFP_KERNEL);
if (!ctx->domainname) { if (!ctx->domainname) {
cifs_dbg(FYI, "Unable to allocate %zd bytes for domain\n", cifs_dbg(FYI, "Unable to allocate %zd bytes for domain\n",
len); len);
...@@ -3411,8 +3409,7 @@ int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx) ...@@ -3411,8 +3409,7 @@ int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
goto error; goto error;
} }
/* Save mount options */ /* Save mount options */
mntdata = kstrndup(cifs_sb->ctx->mount_options, mntdata = kstrdup(cifs_sb->ctx->mount_options, GFP_KERNEL);
strlen(cifs_sb->ctx->mount_options), GFP_KERNEL);
if (!mntdata) { if (!mntdata) {
rc = -ENOMEM; rc = -ENOMEM;
goto error; goto error;
...@@ -3485,7 +3482,7 @@ int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx) ...@@ -3485,7 +3482,7 @@ int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
* links, the prefix path is included in both and may be changed during reconnect. See * links, the prefix path is included in both and may be changed during reconnect. See
* cifs_tree_connect(). * cifs_tree_connect().
*/ */
cifs_sb->origin_fullpath = kstrndup(full_path, strlen(full_path), GFP_KERNEL); cifs_sb->origin_fullpath = kstrdup(full_path, GFP_KERNEL);
if (!cifs_sb->origin_fullpath) { if (!cifs_sb->origin_fullpath) {
rc = -ENOMEM; rc = -ENOMEM;
goto error; goto error;
......
...@@ -89,7 +89,7 @@ static int get_normalized_path(const char *path, char **npath) ...@@ -89,7 +89,7 @@ static int get_normalized_path(const char *path, char **npath)
if (*path == '\\') { if (*path == '\\') {
*npath = (char *)path; *npath = (char *)path;
} else { } else {
*npath = kstrndup(path, strlen(path), GFP_KERNEL); *npath = kstrdup(path, GFP_KERNEL);
if (!*npath) if (!*npath)
return -ENOMEM; return -ENOMEM;
convert_delimiter(*npath, '\\'); convert_delimiter(*npath, '\\');
...@@ -358,7 +358,7 @@ static struct cache_dfs_tgt *alloc_target(const char *name, int path_consumed) ...@@ -358,7 +358,7 @@ static struct cache_dfs_tgt *alloc_target(const char *name, int path_consumed)
t = kmalloc(sizeof(*t), GFP_ATOMIC); t = kmalloc(sizeof(*t), GFP_ATOMIC);
if (!t) if (!t)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
t->name = kstrndup(name, strlen(name), GFP_ATOMIC); t->name = kstrdup(name, GFP_ATOMIC);
if (!t->name) { if (!t->name) {
kfree(t); kfree(t);
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
...@@ -419,7 +419,7 @@ static struct cache_entry *alloc_cache_entry(const char *path, ...@@ -419,7 +419,7 @@ static struct cache_entry *alloc_cache_entry(const char *path,
if (!ce) if (!ce)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
ce->path = kstrndup(path, strlen(path), GFP_KERNEL); ce->path = kstrdup(path, GFP_KERNEL);
if (!ce->path) { if (!ce->path) {
kmem_cache_free(cache_slab, ce); kmem_cache_free(cache_slab, ce);
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
...@@ -531,7 +531,7 @@ static struct cache_entry *lookup_cache_entry(const char *path, unsigned int *ha ...@@ -531,7 +531,7 @@ static struct cache_entry *lookup_cache_entry(const char *path, unsigned int *ha
char *s, *e; char *s, *e;
char sep; char sep;
npath = kstrndup(path, strlen(path), GFP_KERNEL); npath = kstrdup(path, GFP_KERNEL);
if (!npath) if (!npath)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
...@@ -641,7 +641,7 @@ static int __update_cache_entry(const char *path, ...@@ -641,7 +641,7 @@ static int __update_cache_entry(const char *path,
if (ce->tgthint) { if (ce->tgthint) {
s = ce->tgthint->name; s = ce->tgthint->name;
th = kstrndup(s, strlen(s), GFP_ATOMIC); th = kstrdup(s, GFP_ATOMIC);
if (!th) if (!th)
return -ENOMEM; return -ENOMEM;
} }
...@@ -786,11 +786,11 @@ static int setup_referral(const char *path, struct cache_entry *ce, ...@@ -786,11 +786,11 @@ static int setup_referral(const char *path, struct cache_entry *ce,
memset(ref, 0, sizeof(*ref)); memset(ref, 0, sizeof(*ref));
ref->path_name = kstrndup(path, strlen(path), GFP_ATOMIC); ref->path_name = kstrdup(path, GFP_ATOMIC);
if (!ref->path_name) if (!ref->path_name)
return -ENOMEM; return -ENOMEM;
ref->node_name = kstrndup(target, strlen(target), GFP_ATOMIC); ref->node_name = kstrdup(target, GFP_ATOMIC);
if (!ref->node_name) { if (!ref->node_name) {
rc = -ENOMEM; rc = -ENOMEM;
goto err_free_path; goto err_free_path;
...@@ -828,7 +828,7 @@ static int get_targets(struct cache_entry *ce, struct dfs_cache_tgt_list *tl) ...@@ -828,7 +828,7 @@ static int get_targets(struct cache_entry *ce, struct dfs_cache_tgt_list *tl)
goto err_free_it; goto err_free_it;
} }
it->it_name = kstrndup(t->name, strlen(t->name), GFP_ATOMIC); it->it_name = kstrdup(t->name, GFP_ATOMIC);
if (!it->it_name) { if (!it->it_name) {
kfree(it); kfree(it);
rc = -ENOMEM; rc = -ENOMEM;
...@@ -1166,7 +1166,7 @@ int dfs_cache_add_vol(char *mntdata, struct smb3_fs_context *ctx, const char *fu ...@@ -1166,7 +1166,7 @@ int dfs_cache_add_vol(char *mntdata, struct smb3_fs_context *ctx, const char *fu
if (!vi) if (!vi)
return -ENOMEM; return -ENOMEM;
vi->fullpath = kstrndup(fullpath, strlen(fullpath), GFP_KERNEL); vi->fullpath = kstrdup(fullpath, GFP_KERNEL);
if (!vi->fullpath) { if (!vi->fullpath) {
rc = -ENOMEM; rc = -ENOMEM;
goto err_free_vi; goto err_free_vi;
......
...@@ -430,7 +430,7 @@ int smb3_parse_opt(const char *options, const char *key, char **val) ...@@ -430,7 +430,7 @@ int smb3_parse_opt(const char *options, const char *key, char **val)
if (nval == p) if (nval == p)
continue; continue;
*nval++ = 0; *nval++ = 0;
*val = kstrndup(nval, strlen(nval), GFP_KERNEL); *val = kstrdup(nval, GFP_KERNEL);
rc = !*val ? -ENOMEM : 0; rc = !*val ? -ENOMEM : 0;
goto out; goto out;
} }
......
...@@ -1180,7 +1180,7 @@ int update_super_prepath(struct cifs_tcon *tcon, char *prefix) ...@@ -1180,7 +1180,7 @@ int update_super_prepath(struct cifs_tcon *tcon, char *prefix)
kfree(cifs_sb->prepath); kfree(cifs_sb->prepath);
if (prefix && *prefix) { if (prefix && *prefix) {
cifs_sb->prepath = kstrndup(prefix, strlen(prefix), GFP_ATOMIC); cifs_sb->prepath = kstrdup(prefix, GFP_ATOMIC);
if (!cifs_sb->prepath) { if (!cifs_sb->prepath) {
rc = -ENOMEM; rc = -ENOMEM;
goto out; goto out;
......
...@@ -926,9 +926,7 @@ cifs_unix_dfs_readlink(const unsigned int xid, struct cifs_tcon *tcon, ...@@ -926,9 +926,7 @@ cifs_unix_dfs_readlink(const unsigned int xid, struct cifs_tcon *tcon,
0); 0);
if (!rc) { if (!rc) {
*symlinkinfo = kstrndup(referral.node_name, *symlinkinfo = kstrdup(referral.node_name, GFP_KERNEL);
strlen(referral.node_name),
GFP_KERNEL);
free_dfs_info_param(&referral); free_dfs_info_param(&referral);
if (!*symlinkinfo) if (!*symlinkinfo)
rc = -ENOMEM; rc = -ENOMEM;
......
...@@ -50,7 +50,6 @@ char *extract_sharename(const char *unc) ...@@ -50,7 +50,6 @@ char *extract_sharename(const char *unc)
{ {
const char *src; const char *src;
char *delim, *dst; char *delim, *dst;
int len;
/* skip double chars at the beginning */ /* skip double chars at the beginning */
src = unc + 2; src = unc + 2;
...@@ -60,10 +59,9 @@ char *extract_sharename(const char *unc) ...@@ -60,10 +59,9 @@ char *extract_sharename(const char *unc)
if (!delim) if (!delim)
return ERR_PTR(-EINVAL); return ERR_PTR(-EINVAL);
delim++; delim++;
len = strlen(delim);
/* caller has to free the memory */ /* caller has to free the memory */
dst = kstrndup(delim, len, GFP_KERNEL); dst = kstrdup(delim, GFP_KERNEL);
if (!dst) if (!dst)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
......
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