Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 1 | // Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <stdio.h> |
| 6 | #include <unistd.h> |
| 7 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame^] | 8 | #include <base/files/file_path.h> |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 9 | #include <base/file_util.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame^] | 10 | #include <base/strings/stringprintf.h> |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 11 | |
Alex Deymo | 6e97bb2 | 2014-02-05 16:46:16 -0800 | [diff] [blame] | 12 | #include "update_engine/policy_manager/real_random_provider.h" |
| 13 | #include "update_engine/policy_manager/variable.h" |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 14 | |
| 15 | using std::string; |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | // The device providing randomness. |
| 20 | const char* kRandomDevice = "/dev/urandom"; |
| 21 | |
| 22 | } // namespace |
| 23 | |
| 24 | namespace chromeos_policy_manager { |
| 25 | |
Gilad Arnold | 12db701 | 2014-01-29 16:45:30 -0800 | [diff] [blame] | 26 | // A random seed variable. |
| 27 | class RandomSeedVariable : public Variable<uint64_t> { |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 28 | public: |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 29 | // RandomSeedVariable is initialized as kVariableModeConst to let the |
| 30 | // EvaluationContext cache the value between different evaluations of the same |
| 31 | // policy request. |
Gilad Arnold | 12db701 | 2014-01-29 16:45:30 -0800 | [diff] [blame] | 32 | RandomSeedVariable(const string& name, FILE* fp) |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 33 | : Variable<uint64_t>(name, kVariableModeConst), fp_(fp) {} |
Gilad Arnold | 12db701 | 2014-01-29 16:45:30 -0800 | [diff] [blame] | 34 | virtual ~RandomSeedVariable() {} |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 35 | |
| 36 | protected: |
Gilad Arnold | 570ecb1 | 2014-01-29 10:52:29 -0800 | [diff] [blame] | 37 | virtual const uint64_t* GetValue(base::TimeDelta /* timeout */, |
| 38 | string* errmsg) { |
| 39 | uint64_t result; |
| 40 | // Aliasing via char pointer abides by the C/C++ strict-aliasing rules. |
| 41 | char* const buf = reinterpret_cast<char*>(&result); |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 42 | unsigned int buf_rd = 0; |
| 43 | |
Gilad Arnold | 570ecb1 | 2014-01-29 10:52:29 -0800 | [diff] [blame] | 44 | while (buf_rd < sizeof(result)) { |
| 45 | int rd = fread(buf + buf_rd, 1, sizeof(result) - buf_rd, fp_.get()); |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 46 | if (rd == 0 || ferror(fp_.get())) { |
| 47 | // Either EOF on fp or read failed. |
Gilad Arnold | 570ecb1 | 2014-01-29 10:52:29 -0800 | [diff] [blame] | 48 | if (errmsg) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame^] | 49 | *errmsg = base::StringPrintf( |
| 50 | "Error reading from the random device: %s", kRandomDevice); |
Gilad Arnold | 570ecb1 | 2014-01-29 10:52:29 -0800 | [diff] [blame] | 51 | } |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 52 | return NULL; |
| 53 | } |
| 54 | buf_rd += rd; |
| 55 | } |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 56 | |
Gilad Arnold | 570ecb1 | 2014-01-29 10:52:29 -0800 | [diff] [blame] | 57 | return new uint64_t(result); |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | private: |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 61 | file_util::ScopedFILE fp_; |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 62 | |
| 63 | DISALLOW_COPY_AND_ASSIGN(RandomSeedVariable); |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 64 | }; |
| 65 | |
Alex Deymo | bb019fe | 2014-02-03 20:12:17 -0800 | [diff] [blame] | 66 | bool RealRandomProvider::DoInit(void) { |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 67 | FILE* fp = fopen(kRandomDevice, "r"); |
| 68 | if (!fp) |
| 69 | return false; |
Alex Deymo | 540d942 | 2014-02-27 11:17:31 -0800 | [diff] [blame] | 70 | set_var_seed(new RandomSeedVariable("seed", fp)); |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 71 | return true; |
| 72 | } |
| 73 | |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 74 | } // namespace chromeos_policy_manager |