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 | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 10 | #include <base/files/file_path.h> |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 11 | #include <base/files/scoped_file.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 12 | #include <base/strings/stringprintf.h> |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 13 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 14 | #include "update_engine/update_manager/real_random_provider.h" |
| 15 | #include "update_engine/update_manager/variable.h" |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 16 | |
| 17 | using std::string; |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | // The device providing randomness. |
| 22 | const char* kRandomDevice = "/dev/urandom"; |
| 23 | |
| 24 | } // namespace |
| 25 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 26 | namespace chromeos_update_manager { |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 27 | |
Gilad Arnold | 12db701 | 2014-01-29 16:45:30 -0800 | [diff] [blame] | 28 | // A random seed variable. |
| 29 | class RandomSeedVariable : public Variable<uint64_t> { |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 30 | public: |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 31 | // RandomSeedVariable is initialized as kVariableModeConst to let the |
| 32 | // EvaluationContext cache the value between different evaluations of the same |
| 33 | // policy request. |
Gilad Arnold | 12db701 | 2014-01-29 16:45:30 -0800 | [diff] [blame] | 34 | RandomSeedVariable(const string& name, FILE* fp) |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 35 | : Variable<uint64_t>(name, kVariableModeConst), fp_(fp) {} |
Gilad Arnold | 12db701 | 2014-01-29 16:45:30 -0800 | [diff] [blame] | 36 | virtual ~RandomSeedVariable() {} |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 37 | |
| 38 | protected: |
Gilad Arnold | 570ecb1 | 2014-01-29 10:52:29 -0800 | [diff] [blame] | 39 | virtual const uint64_t* GetValue(base::TimeDelta /* timeout */, |
| 40 | string* errmsg) { |
| 41 | uint64_t result; |
| 42 | // Aliasing via char pointer abides by the C/C++ strict-aliasing rules. |
| 43 | char* const buf = reinterpret_cast<char*>(&result); |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 44 | unsigned int buf_rd = 0; |
| 45 | |
Gilad Arnold | 570ecb1 | 2014-01-29 10:52:29 -0800 | [diff] [blame] | 46 | while (buf_rd < sizeof(result)) { |
| 47 | 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] | 48 | if (rd == 0 || ferror(fp_.get())) { |
| 49 | // Either EOF on fp or read failed. |
Gilad Arnold | 570ecb1 | 2014-01-29 10:52:29 -0800 | [diff] [blame] | 50 | if (errmsg) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 51 | *errmsg = base::StringPrintf( |
| 52 | "Error reading from the random device: %s", kRandomDevice); |
Gilad Arnold | 570ecb1 | 2014-01-29 10:52:29 -0800 | [diff] [blame] | 53 | } |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 54 | return nullptr; |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 55 | } |
| 56 | buf_rd += rd; |
| 57 | } |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 58 | |
Gilad Arnold | 570ecb1 | 2014-01-29 10:52:29 -0800 | [diff] [blame] | 59 | return new uint64_t(result); |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | private: |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 63 | base::ScopedFILE fp_; |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 64 | |
| 65 | DISALLOW_COPY_AND_ASSIGN(RandomSeedVariable); |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 66 | }; |
| 67 | |
Alex Deymo | 42c30c3 | 2014-04-24 18:41:18 -0700 | [diff] [blame] | 68 | bool RealRandomProvider::Init(void) { |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 69 | FILE* fp = fopen(kRandomDevice, "r"); |
| 70 | if (!fp) |
| 71 | return false; |
David Zeuthen | 21716e2 | 2014-04-23 15:42:05 -0700 | [diff] [blame] | 72 | var_seed_.reset(new RandomSeedVariable("seed", fp)); |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 73 | return true; |
| 74 | } |
| 75 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 76 | } // namespace chromeos_update_manager |