From 5723e501abb675701352eb3e9fd58bb3cc2c649d Mon Sep 17 00:00:00 2001 From: Jacques Vidrine Date: Tue, 29 Apr 2003 21:13:50 +0000 Subject: [PATCH] `Hide' strlcpy and strlcat (using the namespace.h / __weak_reference technique) so that we don't wind up calling into an application's version if the application defines them. Inspired by: qpopper's interfering and buggy version of strlcpy --- lib/libc/gen/check_utility_compat.c | 9 +++------ lib/libc/gen/confstr.c | 5 +++-- lib/libc/gen/fmtmsg.c | 22 ++++++++++++---------- lib/libc/gen/getgrent.c | 4 ++-- lib/libc/gen/getpwent.c | 10 +++++----- lib/libc/include/namespace.h | 2 ++ lib/libc/include/un-namespace.h | 2 ++ lib/libc/locale/setlocale.c | 4 +++- lib/libc/net/getaddrinfo.c | 4 ++-- lib/libc/net/gethostbydns.c | 6 ++++-- lib/libc/net/getnameinfo.c | 12 +++++++----- lib/libc/net/hesiod.c | 4 +++- lib/libc/net/if_nametoindex.c | 2 +- lib/libc/nls/msgcat.c | 2 +- lib/libc/rpc/rpc_soc.c | 2 +- lib/libc/stdlib/realpath.c | 12 ++++++------ lib/libc/string/strerror.c | 8 +++++--- lib/libc/string/strlcat.c | 5 ++++- lib/libc/string/strlcpy.c | 5 ++++- lib/libc/yp/yplib.c | 2 +- 20 files changed, 71 insertions(+), 51 deletions(-) diff --git a/lib/libc/gen/check_utility_compat.c b/lib/libc/gen/check_utility_compat.c index 0ccdec115a8..2a0db435975 100644 --- a/lib/libc/gen/check_utility_compat.c +++ b/lib/libc/gen/check_utility_compat.c @@ -30,15 +30,12 @@ #include __FBSDID("$FreeBSD$"); -/* - * I din't use "namespace.h" here because none of the relevant utilities - * are threaded, so I'm not concerned about cancellation points or other - * niceties. - */ +#include "namespace.h" #include #include #include #include +#include "un-namespace.h" #ifndef LINE_MAX #define LINE_MAX _POSIX2_LINE_MAX @@ -55,7 +52,7 @@ check_utility_compat(const char *utility) int len; if ((p = getenv(_ENV_UTIL_COMPAT)) != NULL) { - strlcpy(buf, p, sizeof buf); + _strlcpy(buf, p, sizeof buf); } else { if ((len = readlink(_PATH_UTIL_COMPAT, buf, sizeof buf)) < 0) return 0; diff --git a/lib/libc/gen/confstr.c b/lib/libc/gen/confstr.c index 966c3fe2ae1..2885afd3bd3 100644 --- a/lib/libc/gen/confstr.c +++ b/lib/libc/gen/confstr.c @@ -37,13 +37,14 @@ static char sccsid[] = "@(#)confstr.c 8.1 (Berkeley) 6/4/93"; #include __FBSDID("$FreeBSD$"); +#include "namespace.h" #include - #include #include #include #include #include +#include "un-namespace.h" size_t @@ -114,7 +115,7 @@ confstr(int name, char *buf, size_t len) docopy: if (len != 0 && buf != NULL) - strlcpy(buf, p, len); + _strlcpy(buf, p, len); return (strlen(p) + 1); default: diff --git a/lib/libc/gen/fmtmsg.c b/lib/libc/gen/fmtmsg.c index 6caabbbb884..a9b4e60f1e0 100644 --- a/lib/libc/gen/fmtmsg.c +++ b/lib/libc/gen/fmtmsg.c @@ -27,10 +27,12 @@ #include __FBSDID("$FreeBSD$"); +#include "namespace.h" #include #include #include #include +#include "un-namespace.h" /* Default value for MSGVERB. */ #define DFLT_MSGVERB "label:severity:text:action:tag" @@ -101,13 +103,13 @@ fmtmsg(long class, const char *label, int sev, const char *text, #define INSERT_COLON \ if (*output != '\0') \ - strlcat(output, ": ", size) + _strlcat(output, ": ", size) #define INSERT_NEWLINE \ if (*output != '\0') \ - strlcat(output, "\n", size) + _strlcat(output, "\n", size) #define INSERT_SPACE \ if (*output != '\0') \ - strlcat(output, " ", size) + _strlcat(output, " ", size) /* * Returns NULL on memory allocation failure, otherwise returns a pointer to @@ -139,20 +141,20 @@ printfmt(char *msgverb, long class, const char *label, int sev, while ((comp = nextcomp(msgverb)) != NULL) { if (strcmp(comp, "label") == 0 && label != MM_NULLLBL) { INSERT_COLON; - strlcat(output, label, size); + _strlcat(output, label, size); } else if (strcmp(comp, "severity") == 0 && sevname != NULL) { INSERT_COLON; - strlcat(output, sevinfo(sev), size); + _strlcat(output, sevinfo(sev), size); } else if (strcmp(comp, "text") == 0 && text != MM_NULLTXT) { INSERT_COLON; - strlcat(output, text, size); + _strlcat(output, text, size); } else if (strcmp(comp, "action") == 0 && act != MM_NULLACT) { INSERT_NEWLINE; - strlcat(output, "TO FIX: ", size); - strlcat(output, act, size); + _strlcat(output, "TO FIX: ", size); + _strlcat(output, act, size); } else if (strcmp(comp, "tag") == 0 && tag != MM_NULLTAG) { INSERT_SPACE; - strlcat(output, tag, size); + _strlcat(output, tag, size); } } INSERT_NEWLINE; @@ -171,7 +173,7 @@ nextcomp(const char *msgverb) char *retval; if (*lmsgverb == '\0') { - strlcpy(lmsgverb, msgverb, sizeof(lmsgverb)); + _strlcpy(lmsgverb, msgverb, sizeof(lmsgverb)); retval = strtok_r(lmsgverb, ":", &state); } else { retval = strtok_r(NULL, ":", &state); diff --git a/lib/libc/gen/getgrent.c b/lib/libc/gen/getgrent.c index d834d09fc9e..da505741134 100644 --- a/lib/libc/gen/getgrent.c +++ b/lib/libc/gen/getgrent.c @@ -620,7 +620,7 @@ dns_group(void *retval, void *mdata, va_list ap) * pointer for the member list terminator. */ adjsize = bufsize - _ALIGNBYTES - sizeof(char *); - linesize = strlcpy(buffer, hes[0], adjsize); + linesize = _strlcpy(buffer, hes[0], adjsize); if (linesize >= adjsize) { *errnop = ERANGE; rv = NS_RETURN; @@ -721,7 +721,7 @@ nis_group(void *retval, void *mdata, va_list ap) rv = NS_NOTFOUND; switch (how) { case nss_lt_name: - if (strlcpy(buffer, name, bufsize) >= bufsize) + if (_strlcpy(buffer, name, bufsize) >= bufsize) goto erange; break; case nss_lt_id: diff --git a/lib/libc/gen/getpwent.c b/lib/libc/gen/getpwent.c index 427a736a23a..256595b1168 100644 --- a/lib/libc/gen/getpwent.c +++ b/lib/libc/gen/getpwent.c @@ -584,7 +584,7 @@ files_passwd(void *retval, void *mdata, va_list ap) /* MAXLOGNAME includes NUL byte, but we do not * include the NUL byte in the key. */ - namesize = strlcpy(&keybuf[1], name, sizeof(keybuf)-1); + namesize = _strlcpy(&keybuf[1], name, sizeof(keybuf)-1); if (namesize >= sizeof(keybuf)-1) { *errnop = EINVAL; rv = NS_NOTFOUND; @@ -897,7 +897,7 @@ dns_passwd(void *retval, void *mdata, va_list ap) hes = NULL; continue; } - linesize = strlcpy(buffer, hes[0], bufsize); + linesize = _strlcpy(buffer, hes[0], bufsize); if (linesize >= bufsize) { *errnop = ERANGE; rv = NS_RETURN; @@ -1055,7 +1055,7 @@ nis_passwd(void *retval, void *mdata, va_list ap) rv = NS_NOTFOUND; switch (how) { case nss_lt_name: - if (strlcpy(buffer, name, bufsize) >= bufsize) + if (_strlcpy(buffer, name, bufsize) >= bufsize) goto erange; break; case nss_lt_id: @@ -1215,7 +1215,7 @@ compat_use_template(struct passwd *pwd, struct passwd *template, char *buffer, hold.field = NULL; \ else { \ hold.field = p; \ - p += strlcpy(p, pwd->field, eob-p) + 1; \ + p += _strlcpy(p, pwd->field, eob-p) + 1; \ } \ } while (0) COPY(pw_name); @@ -1233,7 +1233,7 @@ compat_use_template(struct passwd *pwd, struct passwd *template, char *buffer, pwd->field = NULL; \ else { \ pwd->field = p; \ - if ((n = strlcpy(p, q, eob-p)) >= eob-p) { \ + if ((n = _strlcpy(p, q, eob-p)) >= eob-p) { \ free(copy); \ return (ERANGE); \ } \ diff --git a/lib/libc/include/namespace.h b/lib/libc/include/namespace.h index e626200e1e2..6a7c68dfb32 100644 --- a/lib/libc/include/namespace.h +++ b/lib/libc/include/namespace.h @@ -43,6 +43,8 @@ #define err _err #define warn _warn #define nsdispatch _nsdispatch +#define strlcat _strlcat +#define strlcpy _strlcpy /* * Prototypes for syscalls/functions that need to be overridden diff --git a/lib/libc/include/un-namespace.h b/lib/libc/include/un-namespace.h index eaa947111c4..f354f78bc45 100644 --- a/lib/libc/include/un-namespace.h +++ b/lib/libc/include/un-namespace.h @@ -149,5 +149,7 @@ int _flock(int, int); #undef err #undef warn #undef nsdispatch +#undef strlcat +#undef strlcpy #endif /* _UN_NAMESPACE_H_ */ diff --git a/lib/libc/locale/setlocale.c b/lib/libc/locale/setlocale.c index 525ca8ff6f2..ca550aead86 100644 --- a/lib/libc/locale/setlocale.c +++ b/lib/libc/locale/setlocale.c @@ -41,6 +41,7 @@ static char sccsid[] = "@(#)setlocale.c 8.1 (Berkeley) 7/4/93"; #include __FBSDID("$FreeBSD$"); +#include "namespace.h" #include #include #include @@ -50,6 +51,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include "un-namespace.h" #include "collate.h" #include "lmonetary.h" /* for __monetary_load_locale() */ #include "lnumeric.h" /* for __numeric_load_locale() */ @@ -180,7 +182,7 @@ setlocale(category, locale) errno = EINVAL; return (NULL); } - (void)strlcpy(new_categories[i], locale, + (void)_strlcpy(new_categories[i], locale, len + 1); i++; locale = r; diff --git a/lib/libc/net/getaddrinfo.c b/lib/libc/net/getaddrinfo.c index e2cb71aae15..2b8708a193c 100644 --- a/lib/libc/net/getaddrinfo.c +++ b/lib/libc/net/getaddrinfo.c @@ -853,7 +853,7 @@ get_canonname(pai, ai, str) ai->ai_canonname = (char *)malloc(strlen(str) + 1); if (ai->ai_canonname == NULL) return EAI_MEMORY; - strlcpy(ai->ai_canonname, str, strlen(str) + 1); + _strlcpy(ai->ai_canonname, str, strlen(str) + 1); } return 0; } @@ -1305,7 +1305,7 @@ getanswer(answer, anslen, qname, qtype, pai) had_error++; continue; } - strlcpy(bp, tbuf, ep - bp); + _strlcpy(bp, tbuf, ep - bp); canonname = bp; bp += n; continue; diff --git a/lib/libc/net/gethostbydns.c b/lib/libc/net/gethostbydns.c index 02cd223b243..9eeba2f6485 100644 --- a/lib/libc/net/gethostbydns.c +++ b/lib/libc/net/gethostbydns.c @@ -60,6 +60,7 @@ static char fromrcsid[] = "From: Id: gethnamaddr.c,v 8.23 1998/04/07 04:59:46 vi #include __FBSDID("$FreeBSD$"); +#include "namespace.h" #include #include #include @@ -78,6 +79,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include "un-namespace.h" #include "res_config.h" @@ -674,7 +676,7 @@ _dns_gethostbyaddr(void *rval, void *cb_data, va_list ap) uaddr[n] & 0xf, (uaddr[n] >> 4) & 0xf)); } - strlcat(qbuf, "ip6.arpa", sizeof(qbuf)); + _strlcat(qbuf, "ip6.arpa", sizeof(qbuf)); break; default: abort(); @@ -686,7 +688,7 @@ _dns_gethostbyaddr(void *rval, void *cb_data, va_list ap) n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf->buf, sizeof buf->buf); if (n < 0 && af == AF_INET6) { *qp = '\0'; - strlcat(qbuf, "ip6.int", sizeof(qbuf)); + _strlcat(qbuf, "ip6.int", sizeof(qbuf)); n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf->buf, sizeof buf->buf); } diff --git a/lib/libc/net/getnameinfo.c b/lib/libc/net/getnameinfo.c index cee07f6e0fe..1aec5be4abe 100644 --- a/lib/libc/net/getnameinfo.c +++ b/lib/libc/net/getnameinfo.c @@ -46,6 +46,7 @@ #include __FBSDID("$FreeBSD$"); +#include "namespace.h" #include #include #include @@ -57,6 +58,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include "un-namespace.h" static const struct afd { int a_af; @@ -145,12 +147,12 @@ getnameinfo(sa, salen, host, hostlen, serv, servlen, flags) if (sp) { if (strlen(sp->s_name) + 1 > servlen) return EAI_MEMORY; - strlcpy(serv, sp->s_name, servlen); + _strlcpy(serv, sp->s_name, servlen); } else { snprintf(numserv, sizeof(numserv), "%u", ntohs(port)); if (strlen(numserv) + 1 > servlen) return EAI_MEMORY; - strlcpy(serv, numserv, servlen); + _strlcpy(serv, numserv, servlen); } } @@ -223,7 +225,7 @@ getnameinfo(sa, salen, host, hostlen, serv, servlen, flags) numaddrlen = strlen(numaddr); if (numaddrlen + 1 > hostlen) /* don't forget terminator */ return EAI_MEMORY; - strlcpy(host, numaddr, hostlen); + _strlcpy(host, numaddr, hostlen); break; } } else { @@ -246,7 +248,7 @@ getnameinfo(sa, salen, host, hostlen, serv, servlen, flags) freehostent(hp); return EAI_MEMORY; } - strlcpy(host, hp->h_name, hostlen); + _strlcpy(host, hp->h_name, hostlen); freehostent(hp); } else { if (flags & NI_NAMEREQD) @@ -293,7 +295,7 @@ ip6_parsenumeric(sa, addr, host, hostlen, flags) numaddrlen = strlen(numaddr); if (numaddrlen + 1 > hostlen) /* don't forget terminator */ return EAI_MEMORY; - strlcpy(host, numaddr, hostlen); + _strlcpy(host, numaddr, hostlen); if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) { char zonebuf[MAXHOSTNAMELEN]; diff --git a/lib/libc/net/hesiod.c b/lib/libc/net/hesiod.c index c22bdbad836..214f86ef9b7 100644 --- a/lib/libc/net/hesiod.c +++ b/lib/libc/net/hesiod.c @@ -51,6 +51,7 @@ static char *orig_rcsid = "$NetBSD: hesiod.c,v 1.9 1999/02/11 06:16:38 simonb Ex #include __FBSDID("$FreeBSD$"); +#include "namespace.h" #include #include #include @@ -64,6 +65,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include "un-namespace.h" struct hesiod_p { char *lhs; /* normally ".ns" */ @@ -163,7 +165,7 @@ hesiod_to_bind(void *context, const char *name, const char *type) const char *rhs; int len; - if (strlcpy(bindname, name, sizeof(bindname)) >= sizeof(bindname)) { + if (_strlcpy(bindname, name, sizeof(bindname)) >= sizeof(bindname)) { errno = EMSGSIZE; return NULL; } diff --git a/lib/libc/net/if_nametoindex.c b/lib/libc/net/if_nametoindex.c index d0ca5212755..887d64f3763 100644 --- a/lib/libc/net/if_nametoindex.c +++ b/lib/libc/net/if_nametoindex.c @@ -70,7 +70,7 @@ if_nametoindex(const char *ifname) s = _socket(AF_INET, SOCK_DGRAM, 0); if (s != -1) { - strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); + _strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); if (_ioctl(s, SIOCGIFINDEX, &ifr) != -1) { _close(s); return (ifr.ifr_index); diff --git a/lib/libc/nls/msgcat.c b/lib/libc/nls/msgcat.c index 4d446184eaf..55ed59ba64f 100644 --- a/lib/libc/nls/msgcat.c +++ b/lib/libc/nls/msgcat.c @@ -160,7 +160,7 @@ catopen(name, type) put_tmpptr: spcleft = sizeof(path) - (pathP - path) - 1; - if (strlcpy(pathP, tmpptr, spcleft) >= + if (_strlcpy(pathP, tmpptr, spcleft) >= spcleft) { too_long: free(plang); diff --git a/lib/libc/rpc/rpc_soc.c b/lib/libc/rpc/rpc_soc.c index 9a58fc59246..13a68f67178 100644 --- a/lib/libc/rpc/rpc_soc.c +++ b/lib/libc/rpc/rpc_soc.c @@ -535,7 +535,7 @@ svcunix_create(sock, sendsize, recvsize, path) memset(&sun, 0, sizeof sun); sun.sun_family = AF_LOCAL; - if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >= + if (_strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) goto done; sun.sun_len = SUN_LEN(&sun); diff --git a/lib/libc/stdlib/realpath.c b/lib/libc/stdlib/realpath.c index c2694717a9e..93a98561fc2 100644 --- a/lib/libc/stdlib/realpath.c +++ b/lib/libc/stdlib/realpath.c @@ -67,14 +67,14 @@ realpath(const char *path, char resolved[PATH_MAX]) if (path[1] == '\0') return (resolved); resolved_len = 1; - left_len = strlcpy(left, path + 1, sizeof(left)); + left_len = _strlcpy(left, path + 1, sizeof(left)); } else { if (getcwd(resolved, PATH_MAX) == NULL) { - strlcpy(resolved, ".", PATH_MAX); + _strlcpy(resolved, ".", PATH_MAX); return (NULL); } resolved_len = strlen(resolved); - left_len = strlcpy(left, path, sizeof(left)); + left_len = _strlcpy(left, path, sizeof(left)); } if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) { errno = ENAMETOOLONG; @@ -131,7 +131,7 @@ realpath(const char *path, char resolved[PATH_MAX]) * lstat() fails we still can return successfully if * there are no more path components left. */ - resolved_len = strlcat(resolved, next_token, PATH_MAX); + resolved_len = _strlcat(resolved, next_token, PATH_MAX); if (resolved_len >= PATH_MAX) { errno = ENAMETOOLONG; return (NULL); @@ -177,13 +177,13 @@ realpath(const char *path, char resolved[PATH_MAX]) symlink[slen] = '/'; symlink[slen + 1] = 0; } - left_len = strlcat(symlink, left, sizeof(left)); + left_len = _strlcat(symlink, left, sizeof(left)); if (left_len >= sizeof(left)) { errno = ENAMETOOLONG; return (NULL); } } - left_len = strlcpy(left, symlink, sizeof(left)); + left_len = _strlcpy(left, symlink, sizeof(left)); } } diff --git a/lib/libc/string/strerror.c b/lib/libc/string/strerror.c index 02fba0cfcd3..0c20ac38c28 100644 --- a/lib/libc/string/strerror.c +++ b/lib/libc/string/strerror.c @@ -37,9 +37,11 @@ static char sccsid[] = "@(#)strerror.c 8.1 (Berkeley) 6/4/93"; #include __FBSDID("$FreeBSD$"); +#include "namespace.h" #include #include #include +#include "un-namespace.h" #define UPREFIX "Unknown error: " @@ -69,8 +71,8 @@ errstr(int num, char *buf, size_t len) } while (uerr /= 10); if (num < 0) *--t = '-'; - strlcpy(buf, UPREFIX, len); - strlcat(buf, t, len); + _strlcpy(buf, UPREFIX, len); + _strlcat(buf, t, len); } int @@ -81,7 +83,7 @@ strerror_r(int errnum, char *strerrbuf, size_t buflen) errstr(errnum, strerrbuf, buflen); return (EINVAL); } - if (strlcpy(strerrbuf, sys_errlist[errnum], buflen) >= buflen) + if (_strlcpy(strerrbuf, sys_errlist[errnum], buflen) >= buflen) return (ERANGE); return (0); } diff --git a/lib/libc/string/strlcat.c b/lib/libc/string/strlcat.c index 2c2fa568e22..cd9fc433b3f 100644 --- a/lib/libc/string/strlcat.c +++ b/lib/libc/string/strlcat.c @@ -33,9 +33,12 @@ static char *rcsid = "$OpenBSD: strlcat.c,v 1.2 1999/06/17 16:28:58 millert Exp #include __FBSDID("$FreeBSD$"); +#include "namespace.h" #include #include +#include "un-namespace.h" +__weak_reference(_strlcat, strlcat); /* * Appends src to string dst of size siz (unlike strncat, siz is the * full size of dst, not space left). At most siz-1 characters @@ -44,7 +47,7 @@ __FBSDID("$FreeBSD$"); * If retval >= siz, truncation occurred. */ size_t -strlcat(dst, src, siz) +_strlcat(dst, src, siz) char *dst; const char *src; size_t siz; diff --git a/lib/libc/string/strlcpy.c b/lib/libc/string/strlcpy.c index a8a6cb7da86..74f511d7837 100644 --- a/lib/libc/string/strlcpy.c +++ b/lib/libc/string/strlcpy.c @@ -33,15 +33,18 @@ static char *rcsid = "$OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp #include __FBSDID("$FreeBSD$"); +#include "namespace.h" #include #include +#include "un-namespace.h" +__weak_reference(_strlcpy, strlcpy); /* * Copy src to string dst of size siz. At most siz-1 characters * will be copied. Always NUL terminates (unless siz == 0). * Returns strlen(src); if retval >= siz, truncation occurred. */ -size_t strlcpy(dst, src, siz) +size_t _strlcpy(dst, src, siz) char *dst; const char *src; size_t siz; diff --git a/lib/libc/yp/yplib.c b/lib/libc/yp/yplib.c index 6087f2b2b28..b1d5913acc7 100644 --- a/lib/libc/yp/yplib.c +++ b/lib/libc/yp/yplib.c @@ -509,7 +509,7 @@ _yp_dobind(char *dom, struct dom_binding **ypdb) *(u_short *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port; gotit: ysd->dom_vers = YPVERS; - strlcpy(ysd->dom_domain, dom, sizeof(ysd->dom_domain)); + _strlcpy(ysd->dom_domain, dom, sizeof(ysd->dom_domain)); } /* Don't rebuild the connection to the server unless we have to. */ -- 2.45.2