Author: kevlo
Date: Sun May 22 14:31:20 2016
New Revision: 300423
URL: https://svnweb.freebsd.org/changeset/base/300423

Log:
  arc4random() returns 0 to (2**32)−1, use an alternative to initialize
  i_gen if it's zero rather than a divide by 2.
  
  With inputs from  delphij, mckusick, rmacklem
  
  Reviewed by:  mckusick

Modified:
  head/sys/fs/ext2fs/ext2_alloc.c
  head/sys/fs/ext2fs/ext2_vfsops.c
  head/sys/ufs/ffs/ffs_alloc.c
  head/sys/ufs/ffs/ffs_vfsops.c

Modified: head/sys/fs/ext2fs/ext2_alloc.c
==============================================================================
--- head/sys/fs/ext2fs/ext2_alloc.c     Sun May 22 14:13:20 2016        
(r300422)
+++ head/sys/fs/ext2fs/ext2_alloc.c     Sun May 22 14:31:20 2016        
(r300423)
@@ -408,7 +408,8 @@ ext2_valloc(struct vnode *pvp, int mode,
        /*
         * 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;

Modified: head/sys/fs/ext2fs/ext2_vfsops.c
==============================================================================
--- head/sys/fs/ext2fs/ext2_vfsops.c    Sun May 22 14:13:20 2016        
(r300422)
+++ head/sys/fs/ext2fs/ext2_vfsops.c    Sun May 22 14:31:20 2016        
(r300423)
@@ -998,7 +998,8 @@ ext2_vget(struct mount *mp, ino_t ino, i
         * 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;
        }

Modified: head/sys/ufs/ffs/ffs_alloc.c
==============================================================================
--- head/sys/ufs/ffs/ffs_alloc.c        Sun May 22 14:13:20 2016        
(r300422)
+++ head/sys/ufs/ffs/ffs_alloc.c        Sun May 22 14:31:20 2016        
(r300423)
@@ -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++;
                }
                /*

Modified: head/sys/ufs/ffs/ffs_vfsops.c
==============================================================================
--- head/sys/ufs/ffs/ffs_vfsops.c       Sun May 22 14:13:20 2016        
(r300422)
+++ head/sys/ufs/ffs/ffs_vfsops.c       Sun May 22 14:31:20 2016        
(r300423)
@@ -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);
_______________________________________________
svn-src-head@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to