diff options
author | Jeff Layton <jlayton@samba.org> | 2012-10-29 15:45:37 -0400 |
---|---|---|
committer | Jeff Layton <jlayton@samba.org> | 2012-10-29 15:45:37 -0400 |
commit | 803feff6aa66c0bb0f0a703eb2404477889a56d5 (patch) | |
tree | e45844717259b10a9fef9e4d26fdfcf3fbac68fb | |
parent | 035f69a9b5fe3c72df73bbbda2d7e570891f971e (diff) | |
download | cifs-utils-803feff6aa66c0bb0f0a703eb2404477889a56d5.tar.gz cifs-utils-803feff6aa66c0bb0f0a703eb2404477889a56d5.tar.bz2 cifs-utils-803feff6aa66c0bb0f0a703eb2404477889a56d5.zip |
cifs.idmap: don't use atoi to convert unsigned int to number
atoi() is for signed integers, and is deprecated in any case. Use
strtoul() instead and check the result carefully before using it.
Also add a log message when the string(s) can't be converted and
fix the signedness of the types in other log messages.
Reviewed-by: Shirish Pargaonkar <shirishpargaonkar@gmail.com>
Signed-off-by: Jeff Layton <jlayton@samba.org>
-rw-r--r-- | cifs.idmap.c | 46 |
1 files changed, 40 insertions, 6 deletions
diff --git a/cifs.idmap.c b/cifs.idmap.c index 457d307..842560b 100644 --- a/cifs.idmap.c +++ b/cifs.idmap.c @@ -75,6 +75,28 @@ char *strget(const char *str, const char *substr) return substrptr; } +/* + * Convert a string representation of unsigned int into a numeric one. Also + * check for incomplete string conversion and overflow. + */ +static int +str_to_uint(const char *src, unsigned int *dst) +{ + unsigned long tmp; + char *end; + + errno = 0; + tmp = strtoul(src, &end, 0); + + if (*end != '\0') + return EINVAL; + if (tmp > UINT_MAX) + return EOVERFLOW; + + *dst = (unsigned int)tmp; + return 0; +} + static int cifs_idmap(const key_serial_t key, const char *key_descr) { @@ -138,11 +160,17 @@ cifs_idmap(const key_serial_t key, const char *key_descr) sidstr = strget(key_descr, "oi:"); if (sidstr) { - uid = atoi(sidstr); - syslog(LOG_DEBUG, "SID: %s, uid: %d", sidstr, uid); + rc = str_to_uint(sidstr, (unsigned int *)&uid); + if (rc) { + syslog(LOG_ERR, "Unable to convert %s to uid: %s", + sidstr, strerror(rc)); + goto cifs_idmap_ret; + } + + syslog(LOG_DEBUG, "SID: %s, uid: %u", sidstr, uid); rc = wbcUidToSid(uid, &sid); if (rc) - syslog(LOG_DEBUG, "uid %d to SID error: %d", uid, rc); + syslog(LOG_DEBUG, "uid %u to SID error: %d", uid, rc); if (!rc) { /* SID has been mapped to a uid */ rc = keyctl_instantiate(key, &sid, sizeof(struct wbcDomainSid), 0); @@ -156,11 +184,17 @@ cifs_idmap(const key_serial_t key, const char *key_descr) sidstr = strget(key_descr, "gi:"); if (sidstr) { - gid = atoi(sidstr); - syslog(LOG_DEBUG, "SID: %s, gid: %d", sidstr, gid); + rc = str_to_uint(sidstr, (unsigned int *)&gid); + if (rc) { + syslog(LOG_ERR, "Unable to convert %s to gid: %s", + sidstr, strerror(rc)); + goto cifs_idmap_ret; + } + + syslog(LOG_DEBUG, "SID: %s, gid: %u", sidstr, gid); rc = wbcGidToSid(gid, &sid); if (rc) - syslog(LOG_DEBUG, "gid %d to SID error: %d", gid, rc); + syslog(LOG_DEBUG, "gid %u to SID error: %d", gid, rc); if (!rc) { /* SID has been mapped to a gid */ rc = keyctl_instantiate(key, &sid, sizeof(struct wbcDomainSid), 0); |