~n-muench/ubuntu/precise/open-vm-tools/open-vm-tools.raring-precise.backport

« back to all changes in this revision

Viewing changes to lib/misc/random.c

  • Committer: Package Import Robot
  • Author(s): Nate Muench
  • Date: 2012-01-23 16:09:45 UTC
  • mfrom: (1.4.6) (2.4.26 sid)
  • Revision ID: package-import@ubuntu.com-20120123160945-b6s0r1vkcovucpf3
Tags: 2011.12.20-562307-0ubuntu1
* Merge latest upstream git tag. Fixes building on Precise
  (LP: #898289, LP: #905612)

* Items merged from Debian unstable:
  - debian/control:
    + open-vm-tools recommends open-vm-dkms. (LP: #598933)
    + open-vm-tools now suggests open-vm-toolbox. (LP: #604998)
  (From 2011.08.21-471295-1 release)
  - Updating maintainer and uploaders fields.
  - Removing vcs fields.
  - Removing references to Daniel's old email address.
  - Updating years in copyright file.
  - Updating to standards version 3.9.2.
  - Updating to debhelper version 8.
  - Switching to source format 3.0 (quilt).
  - Removing manual chrpath setting.
  - Removing exclusion from plugins from debhelper shlibs.
  - Rediffing kvers.patch.
  (From 2011.09.23-491607-1 release)
  - Marking binary architecture-dependend packages as linux and kfreebsd
  only.
  - Removing liburiparser-dev from build-depends as upstream dropped
  unity support.
  - Building with libproc-dev on amd64 again.
  - Dropping disabling of dnet support.
  (From 2011.09.23-491607-2 release)
  - Adding doxygen to build-depends for api documentation.
  - Adding libcunit1-dev to build-depends for test suites.
  - Minimizing rules file.
  - Adding open-vm-tools-dev package, containing only the api
    documentation for now.
  (From 2011.09.23-491607-3 release)
  - Sorting overrides in rules alphabetically.
  - Compacting copyright file.
  - Adding udev rule to set timeout for vmware scsi devices
  (From 2011.12.20-562307-1 release)
  - Adding patch to correct typo in upstreams dkms configuration

* Remaining Changes:
  - Remove Stable part of version numbering.
  - debian folder:
    + Re-added open-vm-dkms.postinst & open-vm-dkms.prerm.
      * Allows dkms modules to compile upon installation.
  - debian/control:
    + Re-add open-vm-source and make into a transitional package
      for open-vm-toolbox.
    + Return dependancies that were moved to open-vm-tools back to
      open-vm-toolbox.
  - debian/rules and debian/open-vm-toolbox.lintian-overrides:
    + Make vmware-user-suid-wrapper suid-root
  - debian/rules:
    + Added CFLAGS field with -Wno-deprecated-declarations
      * Will suppress issues with glib 2.31 or later.
    + Add line to copy vmware-xdg-detect-de into place.
    + Install vmware-user.desktop through toolbox package.
  - debian/open-vm-tools.init:
    + Re-add 'modprobe [-r] vmblock'.
    + Add 'modprobe [-r] vmxnet'.
      * Incase it's not loaded during boot.
    + Remove and re-add pcnet32 module
      * Will be done before (remove) and after (readd) vmxnet module
        is added.
      * If vmxnet doesn't exist (aka modules fail to build), pcnet32 can be
        still used for network connectivity.
      * Workaround until a better fix can be done.
  - Re-add gnome-session to debian/local/xautostart.conf
  - Manpages removed (from debian/manpages):
    + vmmemctl.9
    + vmxnet3.9
    + Remove references to manpages that have been removed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
#   include <errno.h>
33
33
#   include <fcntl.h>
34
34
#   include <unistd.h>
 
35
 
 
36
#   define GENERIC_RANDOM_DEVICE "/dev/urandom"
35
37
#endif
36
38
 
37
39
#include "vmware.h"
38
40
#include "random.h"
39
41
 
40
 
#define ESX_RANDOM_DEVICE     "/vmfs/devices/char/vmkdriver/urandom"
41
 
#define GENERIC_RANDOM_DEVICE "/dev/urandom"
42
 
 
43
 
 
 
42
 
 
43
#if defined(_WIN32)
44
44
/*
45
45
 *-----------------------------------------------------------------------------
46
46
 *
47
 
 * Random_Crypto --
 
47
 * RandomBytesWin32 --
48
48
 *
49
49
 *      Generate 'size' bytes of cryptographically strong random bits in
50
 
 *      'buffer'. Use this function when you need non-predictable random
51
 
 *      bits, typically in security applications. Use Random_Quick below
52
 
 *      otherwise.
 
50
 *      'buffer'.
53
51
 *
54
52
 * Results:
55
 
 *      TRUE on success
56
 
 *      FALSE on failure
57
 
 *
58
 
 * Side effects:
59
 
 *      None
 
53
 *      TRUE   success
 
54
 *      FALSE  failure
60
55
 *
61
56
 *-----------------------------------------------------------------------------
62
57
 */
63
 
 
64
 
Bool
65
 
Random_Crypto(unsigned int size,  // IN:
66
 
              void *buffer)       // OUT:
 
58
 
 
59
static Bool
 
60
RandomBytesWin32(unsigned int size,  // IN:
 
61
                 void *buffer)       // OUT:
67
62
{
68
 
#if defined(_WIN32)
69
63
   HCRYPTPROV csp;
70
64
 
71
65
   if (CryptAcquireContext(&csp, NULL, NULL, PROV_RSA_FULL,
72
66
                           CRYPT_VERIFYCONTEXT) == FALSE) {
73
 
      Log("%s: CryptAcquireContext failed %d\n", __FUNCTION__,
74
 
          GetLastError());
75
67
      return FALSE;
76
68
   }
77
69
 
78
70
   if (CryptGenRandom(csp, size, buffer) == FALSE) {
79
71
      CryptReleaseContext(csp, 0);
80
 
      Log("%s: CryptGenRandom failed %d\n", __FUNCTION__, GetLastError());
81
 
 
82
72
      return FALSE;
83
73
   }
84
74
 
85
75
   if (CryptReleaseContext(csp, 0) == FALSE) {
86
 
      Log("%s: CryptReleaseContext failed %d\n", __FUNCTION__,
87
 
          GetLastError());
88
 
 
89
76
      return FALSE;
90
77
   }
91
 
#else
92
 
   int fd;
93
 
 
94
 
   /*
95
 
    * We use /dev/urandom and not /dev/random because it is good enough and
96
 
    * because it cannot block. --hpreg
97
 
    *
98
 
    * Bug 352496: On ESX, /dev/urandom is proxied into COS, and hence is
99
 
    * expensive. The VMkernel RNG is available through VMFS and is not
100
 
    * proxied. So we use that instead.
101
 
    */
102
 
 
103
 
#if defined(VMX86_SERVER)
104
 
   /*
105
 
    * ESX: attempt to use the wonderful random device.
106
 
    */
107
 
 
108
 
   fd = open(ESX_RANDOM_DEVICE, O_RDONLY);
109
 
 
110
 
#if defined(VMX86_DEVEL)
111
 
   /*
112
 
    * On developer builds, attempt to fall back to the generic random
113
 
    * device, even if it is much slower. This has a nice side-effect -
114
 
    * some things built for ESX will actually work in a Linux hosted
115
 
    * environment.
116
 
    */
117
 
 
118
 
   if (fd == -1) {
119
 
      Log("%s: open of %s failed, attempting to use %s\n", __FUNCTION__,
120
 
          ESX_RANDOM_DEVICE, GENERIC_RANDOM_DEVICE);
121
 
 
122
 
      fd = open(GENERIC_RANDOM_DEVICE, O_RDONLY);
123
 
   }
124
 
#endif
125
 
#else
126
 
   fd = open(GENERIC_RANDOM_DEVICE, O_RDONLY);
127
 
#endif
128
 
 
129
 
   if (fd == -1) {
130
 
      Log("%s: Failed to open random device: %d\n", __FUNCTION__, errno);
131
 
 
 
78
 
 
79
   return TRUE;
 
80
}
 
81
#else
 
82
/*
 
83
 *-----------------------------------------------------------------------------
 
84
 *
 
85
 * RandomBytesPosix --
 
86
 *
 
87
 *      Generate 'size' bytes of cryptographically strong random bits in
 
88
 *      'buffer'.
 
89
 *
 
90
 * Results:
 
91
 *      TRUE   success
 
92
 *      FALSE  failure
 
93
 *
 
94
 *-----------------------------------------------------------------------------
 
95
 */
 
96
 
 
97
static Bool
 
98
RandomBytesPosix(const char *name,   // IN:
 
99
                 unsigned int size,  // IN:
 
100
                 void *buffer)       // OUT:
 
101
{
 
102
   int fd = open(name, O_RDONLY);
 
103
 
 
104
   if (fd == -1) {
132
105
      return FALSE;
133
106
   }
134
107
 
135
108
   /* Although /dev/urandom does not block, it can return short reads. */
 
109
 
136
110
   while (size > 0) {
137
111
      ssize_t bytesRead = read(fd, buffer, size);
138
112
 
139
 
      if (bytesRead == 0 || (bytesRead == -1 && errno != EINTR)) {
140
 
         int error = errno;
141
 
 
 
113
      if ((bytesRead == 0) || ((bytesRead == -1) && (errno != EINTR))) {
142
114
         close(fd);
143
 
         Log("%s: Short read: %d\n", __FUNCTION__, error);
144
115
 
145
116
         return FALSE;
146
117
      }
 
118
 
147
119
      if (bytesRead > 0) {
148
120
         size -= bytesRead;
149
121
         buffer = ((uint8 *) buffer) + bytesRead; 
151
123
   }
152
124
 
153
125
   if (close(fd) == -1) {
154
 
      Log("%s: Failed to close: %d\n", __FUNCTION__, errno);
155
 
 
156
126
      return FALSE;
157
127
   }
158
 
#endif
159
128
 
160
129
   return TRUE;
161
130
}
 
131
#endif
 
132
 
 
133
 
 
134
/*
 
135
 *-----------------------------------------------------------------------------
 
136
 *
 
137
 * Random_Crypto --
 
138
 *
 
139
 *      Generate 'size' bytes of cryptographically strong random bits in
 
140
 *      'buffer'. Use this function when you need non-predictable random
 
141
 *      bits, typically in security applications, where the bits are generated
 
142
 *      external to the application.
 
143
 *
 
144
 *      DO NOT USE THIS FUNCTION UNLESS YOU HAVE AN ABSOLUTE, EXPLICIT
 
145
 *      NEED FOR CRYPTOGRAPHICALLY VALID RANDOM NUMBERS.
 
146
 *
 
147
 * Results:
 
148
 *      TRUE   success
 
149
 *      FALSE  failure
 
150
 *
 
151
 * Side effects:
 
152
 *      None
 
153
 *
 
154
 *-----------------------------------------------------------------------------
 
155
 */
 
156
 
 
157
Bool
 
158
Random_Crypto(unsigned int size,  // IN:
 
159
              void *buffer)       // OUT:
 
160
{
 
161
#if defined(_WIN32)
 
162
   return RandomBytesWin32(size, buffer);
 
163
#else
 
164
   /*
 
165
    * We use /dev/urandom and not /dev/random because it is good enough and
 
166
    * because it cannot block. --hpreg
 
167
    */
 
168
 
 
169
   return RandomBytesPosix(GENERIC_RANDOM_DEVICE, size, buffer);
 
170
#endif
 
171
}
 
172
 
162
173
 
163
174
 
164
175
/*