> Date: Sun, 16 Nov 2014 19:10:41 -0500
> From: Ted Unangst <[email protected]>
>
> The historic copystr API is like copyinstr/copyoutstr but only for
> kernel strings. A much more familiar API is strlcpy.
>
> This converts three examples in kern.
>
> One where the return value is unchecked. One where the return is
> checked. And one more complicated conversion where the outlen param is
> used.
>
> Now, something to be careful about is the man page for copystr is
> rather unclear about what actually happens with the nul bytes. It's
> quite explicit, but somehow manages to still not be clear. For
> example, this line:
>
> functions return ENAMETOOLONG if the string is longer than len bytes.
>
> It would seem that sentence includes the nul byte in len, but when one
> speaks of C strings, the nul byte is typically not counted.
The vfs_subr.c changes make sense, but I really think the other
changes make the code significantly harder to read.
> Index: vfs_subr.c
> ===================================================================
> RCS file: /cvs/src/sys/kern/vfs_subr.c,v
> retrieving revision 1.221
> diff -u -p -r1.221 vfs_subr.c
> --- vfs_subr.c 14 Nov 2014 23:26:48 -0000 1.221
> +++ vfs_subr.c 16 Nov 2014 23:51:59 -0000
> @@ -227,8 +227,8 @@ vfs_rootmountalloc(char *fstypename, cha
> mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
> strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
> mp->mnt_stat.f_mntonname[0] = '/';
> - copystr(devname, mp->mnt_stat.f_mntfromname, MNAMELEN, 0);
> - copystr(devname, mp->mnt_stat.f_mntfromspec, MNAMELEN, 0);
> + strlcpy(mp->mnt_stat.f_mntfromname, devname, MNAMELEN);
> + strlcpy(mp->mnt_stat.f_mntfromspec, devname, MNAMELEN);
> *mpp = mp;
> return (0);
> }
> Index: exec_script.c
> ===================================================================
> RCS file: /cvs/src/sys/kern/exec_script.c,v
> retrieving revision 1.31
> diff -u -p -r1.31 exec_script.c
> --- exec_script.c 13 Jul 2014 23:59:58 -0000 1.31
> +++ exec_script.c 16 Nov 2014 23:54:58 -0000
> @@ -232,8 +232,9 @@ check_shell:
> * convenience, not a security issue, we are
> * safe to do this.
> */
> - error = copystr(epp->ep_name, *tmpsap++,
> - MAXPATHLEN, NULL);
> + if (strlcpy(*tmpsap++, epp->ep_name,
> + MAXPATHLEN) >= MAXPATHLEN)
> + error = ENAMETOOLONG;
> } else
> error = copyinstr(epp->ep_name, *tmpsap++, MAXPATHLEN,
> NULL);
> Index: vfs_lookup.c
> ===================================================================
> RCS file: /cvs/src/sys/kern/vfs_lookup.c,v
> retrieving revision 1.47
> diff -u -p -r1.47 vfs_lookup.c
> --- vfs_lookup.c 25 Mar 2014 04:04:36 -0000 1.47
> +++ vfs_lookup.c 16 Nov 2014 23:58:12 -0000
> @@ -113,10 +113,14 @@ namei(struct nameidata *ndp)
> */
> if ((cnp->cn_flags & HASBUF) == 0)
> cnp->cn_pnbuf = pool_get(&namei_pool, PR_WAITOK);
> - if (ndp->ni_segflg == UIO_SYSSPACE)
> - error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
> - MAXPATHLEN, &ndp->ni_pathlen);
> - else
> + if (ndp->ni_segflg == UIO_SYSSPACE) {
> + if ((ndp->ni_pathlen = strlcpy(cnp->cn_pnbuf, ndp->ni_dirp,
> + MAXPATHLEN)) >= MAXPATHLEN) {
> + ndp->ni_pathlen = MAXPATHLEN;
> + error = ENAMETOOLONG;
> + }
> + ndp->ni_pathlen += 1; /* include nul */
> + } else
> error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
> MAXPATHLEN, &ndp->ni_pathlen);
>
>
>