FreeBSD: Fix leaked strings in libspl mnttab

The FreeBSD implementations of various libspl functions for getting
mounted device information were found to leak several strings which
were being allocated in statfs2mnttab but never freed.

The Solaris getmntany(3C) and related interfaces are expected to return
strings residing in static buffers that need to be copied rather than
freed by the caller.

Use static thread-local storage to stash the mnttab structure strings
from FreeBSD's statfs info rather than strings allocated on the heap by
strdup(3).

While here, remove some stray commented out lines.

Reviewed-by: Alexander Motin <mav@FreeBSD.org>
Reviewed-by: Rich Ercolani <rincebrain@gmail.com>
Signed-off-by: Ryan Moeller <ryan@iXsystems.com>
Closes #12961
This commit is contained in:
Ryan Moeller 2022-01-14 15:38:32 -05:00 committed by Tony Hutter
parent c9c9d634aa
commit f4def7ec6c
1 changed files with 16 additions and 9 deletions

View File

@ -91,16 +91,28 @@ optadd(char *mntopts, size_t size, const char *opt)
strlcat(mntopts, opt, size); strlcat(mntopts, opt, size);
} }
static __thread char gfstypename[MFSNAMELEN];
static __thread char gmntfromname[MNAMELEN];
static __thread char gmntonname[MNAMELEN];
static __thread char gmntopts[MNTMAXSTR];
void void
statfs2mnttab(struct statfs *sfs, struct mnttab *mp) statfs2mnttab(struct statfs *sfs, struct mnttab *mp)
{ {
static char mntopts[MNTMAXSTR];
long flags; long flags;
mntopts[0] = '\0'; strlcpy(gfstypename, sfs->f_fstypename, sizeof (gfstypename));
mp->mnt_fstype = gfstypename;
strlcpy(gmntfromname, sfs->f_mntfromname, sizeof (gmntfromname));
mp->mnt_special = gmntfromname;
strlcpy(gmntonname, sfs->f_mntonname, sizeof (gmntonname));
mp->mnt_mountp = gmntonname;
flags = sfs->f_flags; flags = sfs->f_flags;
#define OPTADD(opt) optadd(mntopts, sizeof (mntopts), (opt)) gmntopts[0] = '\0';
#define OPTADD(opt) optadd(gmntopts, sizeof (gmntopts), (opt))
if (flags & MNT_RDONLY) if (flags & MNT_RDONLY)
OPTADD(MNTOPT_RO); OPTADD(MNTOPT_RO);
else else
@ -121,10 +133,7 @@ statfs2mnttab(struct statfs *sfs, struct mnttab *mp)
else else
OPTADD(MNTOPT_EXEC); OPTADD(MNTOPT_EXEC);
#undef OPTADD #undef OPTADD
mp->mnt_special = strdup(sfs->f_mntfromname); mp->mnt_mntopts = gmntopts;
mp->mnt_mountp = strdup(sfs->f_mntonname);
mp->mnt_fstype = strdup(sfs->f_fstypename);
mp->mnt_mntopts = strdup(mntopts);
} }
static struct statfs *gsfs = NULL; static struct statfs *gsfs = NULL;
@ -166,7 +175,6 @@ fail:
int int
getmntany(FILE *fd __unused, struct mnttab *mgetp, struct mnttab *mrefp) getmntany(FILE *fd __unused, struct mnttab *mgetp, struct mnttab *mrefp)
{ {
// struct statfs *sfs;
int i, error; int i, error;
error = statfs_init(); error = statfs_init();
@ -195,7 +203,6 @@ getmntany(FILE *fd __unused, struct mnttab *mgetp, struct mnttab *mrefp)
int int
getmntent(FILE *fp, struct mnttab *mp) getmntent(FILE *fp, struct mnttab *mp)
{ {
// struct statfs *sfs;
int error, nfs; int error, nfs;
nfs = (int)lseek(fileno(fp), 0, SEEK_CUR); nfs = (int)lseek(fileno(fp), 0, SEEK_CUR);