From 57d2ac2f90f124f0b471404fa5178bd6cef08f36 Mon Sep 17 00:00:00 2001 From: Kevin Lo Date: Sun, 22 May 2016 14:31:20 +0000 Subject: [PATCH] =?UTF-8?q?arc4random()=20returns=200=20to=20(2**32)?= =?UTF-8?q?=E2=88=921,=20use=20an=20alternative=20to=20initialize=20i=5Fge?= =?UTF-8?q?n=20if=20it's=20zero=20rather=20than=20a=20divide=20by=202.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With inputs from delphij, mckusick, rmacklem Reviewed by: mckusick --- sys/fs/ext2fs/ext2_alloc.c | 3 ++- sys/fs/ext2fs/ext2_vfsops.c | 3 ++- sys/ufs/ffs/ffs_alloc.c | 7 ++++--- sys/ufs/ffs/ffs_vfsops.c | 3 ++- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/sys/fs/ext2fs/ext2_alloc.c b/sys/fs/ext2fs/ext2_alloc.c index c3bd29ba3c27..35e24c3bde24 100644 --- a/sys/fs/ext2fs/ext2_alloc.c +++ b/sys/fs/ext2fs/ext2_alloc.c @@ -408,7 +408,8 @@ ext2_valloc(struct vnode *pvp, int mode, struct ucred *cred, struct vnode **vpp) /* * Set up a new generation number for this inode. */ - ip->i_gen = arc4random(); + while (ip->i_gen == 0 || ++ip->i_gen == 0) + ip->i_gen = arc4random(); vfs_timestamp(&ts); ip->i_birthtime = ts.tv_sec; diff --git a/sys/fs/ext2fs/ext2_vfsops.c b/sys/fs/ext2fs/ext2_vfsops.c index 9f2b94c1e002..9f73357061de 100644 --- a/sys/fs/ext2fs/ext2_vfsops.c +++ b/sys/fs/ext2fs/ext2_vfsops.c @@ -998,7 +998,8 @@ ext2_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp) * already have one. This should only happen on old filesystems. */ if (ip->i_gen == 0) { - ip->i_gen = random() + 1; + while (ip->i_gen == 0) + ip->i_gen = arc4random(); if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) ip->i_flag |= IN_MODIFIED; } diff --git a/sys/ufs/ffs/ffs_alloc.c b/sys/ufs/ffs/ffs_alloc.c index 797a42b12354..0e4dc0316064 100644 --- a/sys/ufs/ffs/ffs_alloc.c +++ b/sys/ufs/ffs/ffs_alloc.c @@ -1102,8 +1102,8 @@ dup_alloc: /* * Set up a new generation number for this inode. */ - if (ip->i_gen == 0 || ++ip->i_gen == 0) - ip->i_gen = arc4random() / 2 + 1; + while (ip->i_gen == 0 || ++ip->i_gen == 0) + ip->i_gen = arc4random(); DIP_SET(ip, i_gen, ip->i_gen); if (fs->fs_magic == FS_UFS2_MAGIC) { vfs_timestamp(&ts); @@ -2080,7 +2080,8 @@ gotit: bzero(ibp->b_data, (int)fs->fs_bsize); dp2 = (struct ufs2_dinode *)(ibp->b_data); for (i = 0; i < INOPB(fs); i++) { - dp2->di_gen = arc4random() / 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 7ecb053a7cc7..d644b73f4fb9 100644 --- a/sys/ufs/ffs/ffs_vfsops.c +++ b/sys/ufs/ffs/ffs_vfsops.c @@ -1768,7 +1768,8 @@ ffs_vgetf(mp, ino, flags, vpp, ffs_flags) * already have one. This should only happen on old filesystems. */ if (ip->i_gen == 0) { - ip->i_gen = arc4random() / 2 + 1; + while (ip->i_gen == 0) + ip->i_gen = arc4random(); if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { ip->i_flag |= IN_MODIFIED; DIP_SET(ip, i_gen, ip->i_gen);