diff options
author | Otto Moerbeek <otto@cvs.openbsd.org> | 2020-05-28 15:48:30 +0000 |
---|---|---|
committer | Otto Moerbeek <otto@cvs.openbsd.org> | 2020-05-28 15:48:30 +0000 |
commit | e4fe65347da83215efa273f99ad7db13e99f7658 (patch) | |
tree | 671ee987ec5337fa008127dde705192b1715f129 | |
parent | a91ee5b7beebd6cafd108ea10ff85cf84f4f9243 (diff) |
Make generation numbers unsigned and fill them using a random number
from the range [1..UINT_MAX] initially. On inode re-use increment
and on wrap refill from the range [1..UINT_MAX-1] to avoid
assigning UINT_MAX (the original value). Zero still means uninitialized.
ok millert@
-rw-r--r-- | sbin/fsdb/fsdb.c | 10 | ||||
-rw-r--r-- | sbin/newfs/mkfs.c | 12 | ||||
-rw-r--r-- | sys/ufs/ffs/ffs_alloc.c | 18 | ||||
-rw-r--r-- | sys/ufs/ffs/ffs_vfsops.c | 7 | ||||
-rw-r--r-- | sys/ufs/ufs/dinode.h | 4 | ||||
-rw-r--r-- | sys/ufs/ufs/inode.h | 4 |
6 files changed, 26 insertions, 29 deletions
diff --git a/sbin/fsdb/fsdb.c b/sbin/fsdb/fsdb.c index 49128eae52f..d52f01aa7c8 100644 --- a/sbin/fsdb/fsdb.c +++ b/sbin/fsdb/fsdb.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fsdb.c,v 1.32 2018/09/16 02:44:06 millert Exp $ */ +/* $OpenBSD: fsdb.c,v 1.33 2020/05/28 15:48:28 otto Exp $ */ /* $NetBSD: fsdb.c,v 1.7 1997/01/11 06:50:53 lukem Exp $ */ /*- @@ -708,20 +708,20 @@ CMDFUNCSTART(chaflags) CMDFUNCSTART(chgen) { int rval = 1; - long gen; + long long gen; char *cp; if (!checkactive()) return 1; - gen = strtol(argv[1], &cp, 0); + gen = strtoll(argv[1], &cp, 0); if (cp == argv[1] || *cp != '\0' ) { warnx("bad gen `%s'", argv[1]); return 1; } - if (gen > INT_MAX || gen < INT_MIN) { - warnx("gen set beyond 32-bit range of field (%lx)", gen); + if (gen > UINT_MAX || gen < 0) { + warnx("gen set beyond 32-bit range of field (%llx)", gen); return(1); } DIP_SET(curinode, di_gen, gen); diff --git a/sbin/newfs/mkfs.c b/sbin/newfs/mkfs.c index a3fce62a384..4d2c96b889d 100644 --- a/sbin/newfs/mkfs.c +++ b/sbin/newfs/mkfs.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mkfs.c,v 1.99 2020/05/19 12:48:54 sthen Exp $ */ +/* $OpenBSD: mkfs.c,v 1.100 2020/05/28 15:48:29 otto Exp $ */ /* $NetBSD: mkfs.c,v 1.25 1995/06/18 21:35:38 cgd Exp $ */ /* @@ -738,10 +738,10 @@ initcg(int cylno, time_t utime) dp2 = (struct ufs2_dinode *)(&iobuf[start]); for (i = MINIMUM(sblock.fs_ipg, 2 * INOPB(&sblock)); i != 0; i--) { if (sblock.fs_magic == FS_UFS1_MAGIC) { - dp1->di_gen = (u_int32_t)arc4random(); + dp1->di_gen = arc4random(); dp1++; } else { - dp2->di_gen = (u_int32_t)arc4random(); + dp2->di_gen = arc4random(); dp2++; } } @@ -754,7 +754,7 @@ initcg(int cylno, time_t utime) i += sblock.fs_frag) { dp1 = (struct ufs1_dinode *)(&iobuf[start]); for (j = 0; j < INOPB(&sblock); j++) { - dp1->di_gen = (u_int32_t)arc4random(); + dp1->di_gen = arc4random(); dp1++; } wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i), @@ -983,9 +983,9 @@ iput(union dinode *ip, ino_t ino) daddr_t d; if (Oflag <= 1) - ip->dp1.di_gen = (u_int32_t)arc4random(); + ip->dp1.di_gen = arc4random(); else - ip->dp2.di_gen = (u_int32_t)arc4random(); + ip->dp2.di_gen = arc4random(); rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, (char *)&acg); diff --git a/sys/ufs/ffs/ffs_alloc.c b/sys/ufs/ffs/ffs_alloc.c index 4a3267b1d5f..cb0bfeda58a 100644 --- a/sys/ufs/ffs/ffs_alloc.c +++ b/sys/ufs/ffs/ffs_alloc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ffs_alloc.c,v 1.110 2020/02/21 11:13:55 otto Exp $ */ +/* $OpenBSD: ffs_alloc.c,v 1.111 2020/05/28 15:48:29 otto Exp $ */ /* $NetBSD: ffs_alloc.c,v 1.11 1996/05/11 18:27:09 mycroft Exp $ */ /* @@ -413,16 +413,13 @@ ffs_inode_alloc(struct inode *pip, mode_t mode, struct ucred *cred, /* * Set up a new generation number for this inode. - * XXX - just increment for now, this is wrong! (millert) - * Need a way to preserve randomization. + * On wrap, we make sure to assign a number != 0 and != UINT_MAX + * (the origial value). */ if (DIP(ip, gen) != 0) DIP_ADD(ip, gen, 1); - if (DIP(ip, gen) == 0) - DIP_ASSIGN(ip, gen, arc4random() & INT_MAX); - - if (DIP(ip, gen) == 0 || DIP(ip, gen) == -1) - DIP_ASSIGN(ip, gen, 1); /* Shouldn't happen */ + while (DIP(ip, gen) == 0) + DIP_ASSIGN(ip, gen, arc4random_uniform(UINT_MAX)); return (0); @@ -1205,9 +1202,10 @@ gotit: memset(ibp->b_data, 0, fs->fs_bsize); dp2 = (struct ufs2_dinode *)(ibp->b_data); - /* Give each inode a positive generation number */ + /* Give each inode a generation number */ for (i = 0; i < INOPB(fs); i++) { - dp2->di_gen = (arc4random() & INT32_MAX) / 2 + 1; + while (dp2->di_gen == 0) + dp2->di_gen = arc4random(); dp2++; } diff --git a/sys/ufs/ffs/ffs_vfsops.c b/sys/ufs/ffs/ffs_vfsops.c index 453111b39dd..b8a7b0a590b 100644 --- a/sys/ufs/ffs/ffs_vfsops.c +++ b/sys/ufs/ffs/ffs_vfsops.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ffs_vfsops.c,v 1.183 2020/02/21 11:11:15 otto Exp $ */ +/* $OpenBSD: ffs_vfsops.c,v 1.184 2020/05/28 15:48:29 otto Exp $ */ /* $NetBSD: ffs_vfsops.c,v 1.19 1996/02/09 22:22:26 christos Exp $ */ /* @@ -1422,9 +1422,8 @@ retry: * already have one. This should only happen on old filesystems. */ if (DIP(ip, gen) == 0) { - DIP_ASSIGN(ip, gen, arc4random() & INT_MAX); - if (DIP(ip, gen) == 0 || DIP(ip, gen) == -1) - DIP_ASSIGN(ip, gen, 1); /* Shouldn't happen */ + while (DIP(ip, gen) == 0) + DIP_ASSIGN(ip, gen, arc4random()); if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) ip->i_flag |= IN_MODIFIED; } diff --git a/sys/ufs/ufs/dinode.h b/sys/ufs/ufs/dinode.h index 5ad69be286e..7a2d55b45f2 100644 --- a/sys/ufs/ufs/dinode.h +++ b/sys/ufs/ufs/dinode.h @@ -1,4 +1,4 @@ -/* $OpenBSD: dinode.h,v 1.18 2013/05/30 19:19:09 guenther Exp $ */ +/* $OpenBSD: dinode.h,v 1.19 2020/05/28 15:48:29 otto Exp $ */ /* $NetBSD: dinode.h,v 1.7 1995/06/15 23:22:48 cgd Exp $ */ /* @@ -82,7 +82,7 @@ struct ufs1_dinode { int32_t di_ib[NIADDR]; /* 88: Indirect disk blocks. */ u_int32_t di_flags; /* 100: Status flags (chflags). */ int32_t di_blocks; /* 104: Blocks actually held. */ - int32_t di_gen; /* 108: Generation number. */ + u_int32_t di_gen; /* 108: Generation number. */ u_int32_t di_uid; /* 112: File owner. */ u_int32_t di_gid; /* 116: File group. */ int32_t di_spare[2]; /* 120: Reserved; currently unused */ diff --git a/sys/ufs/ufs/inode.h b/sys/ufs/ufs/inode.h index 4675dc7e38d..4530c4e304d 100644 --- a/sys/ufs/ufs/inode.h +++ b/sys/ufs/ufs/inode.h @@ -1,4 +1,4 @@ -/* $OpenBSD: inode.h,v 1.51 2019/01/21 18:09:21 anton Exp $ */ +/* $OpenBSD: inode.h,v 1.52 2020/05/28 15:48:29 otto Exp $ */ /* $NetBSD: inode.h,v 1.8 1995/06/15 23:22:50 cgd Exp $ */ /* @@ -344,6 +344,6 @@ struct ufid { u_int16_t ufid_len; /* Length of structure. */ u_int16_t ufid_pad; /* Force 32-bit alignment. */ ufsino_t ufid_ino; /* File number (ino). */ - int32_t ufid_gen; /* Generation number. */ + u_int32_t ufid_gen; /* Generation number. */ }; #endif /* _KERNEL */ |