OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011, Google Inc. | |
jschuh
2011/09/13 16:37:12
I'd rename this metadata_encrypt_posix.h. Also, I
bxx
2011/09/13 19:36:09
Done.
| |
2 // All rights reserved. | |
3 // | |
4 // Redistribution and use in source and binary forms, with or without | |
5 // modification, are permitted provided that the following conditions are | |
6 // met: | |
7 // | |
8 // * Redistributions of source code must retain the above copyright | |
9 // notice, this list of conditions and the following disclaimer. | |
10 // * Redistributions in binary form must reproduce the above | |
11 // copyright notice, this list of conditions and the following disclaimer | |
12 // in the documentation and/or other materials provided with the | |
13 // distribution. | |
14 // * Neither the name of Google Inc. nor the names of its | |
15 // contributors may be used to endorse or promote products derived from | |
16 // this software without specific prior written permission. | |
17 // | |
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 | |
30 // --- | |
31 // Author: Rebecca Shapiro | |
32 // | |
33 // Generic pointer encryption implementation. | |
34 | |
35 #ifndef TCMALLOC_METADATA_ENCRYPT_GENERIC_H_ | |
36 #define TCMALLOC_METADATA_ENCRYPT_GENERIC_H_ | |
37 | |
38 #include <errno.h> | |
39 #include <fcntl.h> | |
40 #include <stdint.h> | |
41 #include "base/logging.h" | |
42 | |
43 namespace { | |
44 static uintptr_t key_; | |
45 static bool initialized_ = false; | |
jschuh
2011/09/13 16:37:12
You don't need a separate bool for initialized. Ju
bxx
2011/09/13 19:36:09
Done.
| |
46 } | |
47 | |
48 namespace tcmalloc { | |
49 | |
50 void InitEncryption() { | |
51 // Ensure this function only executes once. | |
jar (doing other things)
2011/09/13 18:15:18
You also want assurances that this is only execute
bxx
2011/09/13 20:06:58
Initialization is done when TCMallocGuard() is con
bxx
2011/09/13 20:06:58
It gets initialized when TCMallocGuard() is constr
| |
52 CHECK(initialized_ == false); | |
53 initialized_ == true; | |
jschuh
2011/09/13 16:37:12
Never mark something as "true" until you've actual
jar (doing other things)
2011/09/13 18:15:18
+1 Very good coding style.
On 2011/09/13 16:37:1
bxx
2011/09/13 19:36:09
Done.
| |
54 | |
55 // Read random bytes from urandom to initialize key. | |
56 int fd = open("/dev/urandom", O_RDONLY); | |
57 CHECK(fd > 0); | |
58 size_t uintp_size = sizeof(uintptr_t); | |
59 size_t num_read_bytes = 0; | |
60 while (num_read_bytes < uintp_size) { | |
61 size_t num_bytes = read(fd, reinterpret_cast<void *>(&key_)+num_read_bytes, | |
jschuh
2011/09/13 16:37:12
Don't read directly into key. Better to store it i
bxx
2011/09/13 19:36:09
The initialization is done when TCMallocGuard() is
| |
62 uintp_size-num_read_bytes); | |
63 if (num_bytes < 0) { | |
jar (doing other things)
2011/09/13 18:15:18
Since num_bytes is unsigned, this is a very strang
bxx
2011/09/13 20:06:58
The man pages tell me read() returns -1 when there
| |
64 CHECK( errno == EINTR); | |
65 } else if (num_bytes == 0) { | |
66 break; | |
67 } | |
68 num_read_bytes += num_bytes; | |
69 } | |
70 CHECK(num_read_bytes == uintp_size); | |
71 close(fd); | |
72 } | |
73 | |
74 uintptr_t EncryptUintptr(uintptr_t ptr) { return ptr ^ key_; } | |
75 | |
76 uintptr_t DecryptUintptr(uintptr_t ptr) { return EncryptUintptr(ptr); } | |
77 | |
78 } | |
79 | |
80 #endif // TCMALLOC_METADATA_ENCRYPT_GENERIC_H_ | |
OLD | NEW |