Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/fs/union - avoid another credential leak on error from C...



details:   https://anonhg.NetBSD.org/src/rev/e6b3d281617c
branches:  trunk
changeset: 369984:e6b3d281617c
user:      christos <christos%NetBSD.org@localhost>
date:      Sun Sep 11 15:42:29 2022 +0000

description:
- avoid another credential leak on error from Chris J-D
  (chris at accessvector dot net)
- KNF
- use kmem

diffstat:

 sys/fs/union/union_vfsops.c |  77 +++++++++++++++++++-------------------------
 1 files changed, 34 insertions(+), 43 deletions(-)

diffs (225 lines):

diff -r 5e99a34c51d6 -r e6b3d281617c sys/fs/union/union_vfsops.c
--- a/sys/fs/union/union_vfsops.c       Sun Sep 11 15:31:11 2022 +0000
+++ b/sys/fs/union/union_vfsops.c       Sun Sep 11 15:42:29 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: union_vfsops.c,v 1.81 2020/03/16 21:20:10 pgoyette Exp $       */
+/*     $NetBSD: union_vfsops.c,v 1.82 2022/09/11 15:42:29 christos Exp $       */
 
 /*
  * Copyright (c) 1994 The Regents of the University of California.
@@ -77,7 +77,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: union_vfsops.c,v 1.81 2020/03/16 21:20:10 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: union_vfsops.c,v 1.82 2022/09/11 15:42:29 christos Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -122,7 +122,7 @@
                return EINVAL;
 
 #ifdef UNION_DIAGNOSTIC
-       printf("union_mount(mp = %p)\n", mp);
+       printf("%s(mp = %p)\n", __func__, mp);
 #endif
 
        if (mp->mnt_flag & MNT_GETARGS) {
@@ -154,7 +154,7 @@
         * Find upper node.
         */
        error = namei_simple_user(args->target,
-                               NSM_FOLLOW_NOEMULROOT, &upperrootvp);
+           NSM_FOLLOW_NOEMULROOT, &upperrootvp);
        if (error != 0)
                goto bad;
 
@@ -163,7 +163,7 @@
                goto bad;
        }
 
-       um = kmem_zalloc(sizeof(struct union_mount), KM_SLEEP);
+       um = kmem_zalloc(sizeof(*um), KM_SLEEP);
 
        /*
         * Keep a held reference to the target vnodes.
@@ -246,7 +246,7 @@
        vfs_getnewfsid(mp);
        mp->mnt_lower = um->um_uppervp->v_mount;
 
-       error = set_statvfs_info( path, UIO_USERSPACE, NULL, UIO_USERSPACE,
+       error = set_statvfs_info(path, UIO_USERSPACE, NULL, UIO_USERSPACE,
            mp->mnt_op->vfs_name, mp, l);
        if (error)
                goto bad;
@@ -264,7 +264,7 @@
        default:
                cp = "<invalid>:";
 #ifdef DIAGNOSTIC
-               panic("union_mount: bad um_op");
+               panic("%s: bad um_op", __func__);
 #endif
                break;
        }
@@ -278,7 +278,7 @@
        memset(xp + size, 0, len - size);
 
 #ifdef UNION_DIAGNOSTIC
-       printf("union_mount: from %s, on %s\n",
+       printf("%s: from %s, on %s\n", __func__,
            mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
 #endif
 
@@ -286,16 +286,19 @@
        if (!vn_union_readdir_hook)
                vn_union_readdir_hook = union_readdirhook;
 
-       return (0);
+       return 0;
 
 bad:
-       if (um)
-               kmem_free(um, sizeof(struct union_mount));
+       if (um) {
+               if (um->um_cred)
+                       kauth_cred_free(um->um_cred);
+               kmem_free(um, sizeof(*um));
+       }
        if (upperrootvp)
                vrele(upperrootvp);
        if (lowerrootvp)
                vrele(lowerrootvp);
-       return (error);
+       return error;
 }
 
 /*
@@ -308,7 +311,7 @@
 union_start(struct mount *mp, int flags)
 {
 
-       return (0);
+       return 0;
 }
 
 /*
@@ -333,7 +336,7 @@
        int error;
 
 #ifdef UNION_DIAGNOSTIC
-       printf("union_unmount(mp = %p)\n", mp);
+       printf("%s(mp = %p)\n", __func__, mp);
 #endif
 
        /*
@@ -383,7 +386,7 @@
        /*
         * Finally, throw away the union_mount structure
         */
-       kmem_free(um, sizeof(struct union_mount));
+       kmem_free(um, sizeof(*um));
        mp->mnt_data = NULL;
        return 0;
 }
@@ -401,7 +404,7 @@
        if (um->um_lowervp)
                vref(um->um_lowervp);
        error = union_allocvp(vpp, mp, NULL, NULL, NULL,
-                             um->um_uppervp, um->um_lowervp, 1);
+           um->um_uppervp, um->um_lowervp, 1);
 
        if (error) {
                vrele(um->um_uppervp);
@@ -420,11 +423,11 @@
 {
        int error;
        struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
-       struct statvfs *sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK | M_ZERO);
+       struct statvfs *sbuf = kmem_alloc(sizeof(*sbuf), KM_SLEEP);
        unsigned long lbsize;
 
 #ifdef UNION_DIAGNOSTIC
-       printf("union_statvfs(mp = %p, lvp = %p, uvp = %p)\n", mp,
+       printf("%s(mp = %p, lvp = %p, uvp = %p)\n", __func__, mp,
            um->um_lowervp, um->um_uppervp);
 #endif
 
@@ -468,29 +471,27 @@
 
        copy_statvfs_info(sbp, mp);
 done:
-       free(sbuf, M_TEMP);
+       kmem_free(sbuf, sizeof(*sbuf));
        return error;
 }
 
 /*ARGSUSED*/
 int
-union_sync(struct mount *mp, int waitfor,
-    kauth_cred_t cred)
+union_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
 {
 
        /*
         * XXX - Assumes no data cached at union layer.
         */
-       return (0);
+       return 0;
 }
 
 /*ARGSUSED*/
 int
-union_vget(struct mount *mp, ino_t ino, int lktype,
-    struct vnode **vpp)
+union_vget(struct mount *mp, ino_t ino, int lktype, struct vnode **vpp)
 {
 
-       return (EOPNOTSUPP);
+       return EOPNOTSUPP;
 }
 
 static int
@@ -547,11 +548,11 @@
 {
 
        sysctl_createv(clog, 0, NULL, NULL,
-                      CTLFLAG_PERMANENT,
-                      CTLTYPE_NODE, "union",
-                      SYSCTL_DESCR("Union file system"),
-                      NULL, 0, NULL, 0,
-                      CTL_VFS, 15, CTL_EOL);
+           CTLFLAG_PERMANENT,
+           CTLTYPE_NODE, "union",
+           SYSCTL_DESCR("Union file system"),
+           NULL, 0, NULL, 0,
+           CTL_VFS, 15, CTL_EOL);
        /*
         * XXX the "15" above could be dynamic, thereby eliminating
         * one more instance of the "number to vfs" mapping problem,
@@ -562,23 +563,13 @@
 static int
 union_modcmd(modcmd_t cmd, void *arg)
 {
-       int error;
 
        switch (cmd) {
        case MODULE_CMD_INIT:
-               error = vfs_attach(&union_vfsops);
-               if (error != 0)
-                       break;
-               break;
+               return vfs_attach(&union_vfsops);
        case MODULE_CMD_FINI:
-               error = vfs_detach(&union_vfsops);
-               if (error != 0)
-                       break;
-               break;
+               return vfs_detach(&union_vfsops);
        default:
-               error = ENOTTY;
-               break;
+               return ENOTTY;
        }
-
-       return (error);
 }



Home | Main Index | Thread Index | Old Index