| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2014 The Android Open Source Project | 
|  | 3 | // | 
|  | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | // you may not use this file except in compliance with the License. | 
|  | 6 | // You may obtain a copy of the License at | 
|  | 7 | // | 
|  | 8 | //      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | // | 
|  | 10 | // Unless required by applicable law or agreed to in writing, software | 
|  | 11 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | // See the License for the specific language governing permissions and | 
|  | 14 | // limitations under the License. | 
|  | 15 | // | 
| Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 16 |  | 
| Alex Deymo | aab50e3 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 17 | #include "update_engine/update_manager/real_random_provider.h" | 
|  | 18 |  | 
| Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 19 | #include <stdio.h> | 
|  | 20 | #include <unistd.h> | 
|  | 21 |  | 
| Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 22 | #include <string> | 
|  | 23 |  | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 24 | #include <base/files/file_path.h> | 
| Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 25 | #include <base/files/scoped_file.h> | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 26 | #include <base/strings/stringprintf.h> | 
| Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 27 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 28 | #include "update_engine/update_manager/variable.h" | 
| Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 29 |  | 
|  | 30 | using std::string; | 
|  | 31 |  | 
|  | 32 | namespace { | 
|  | 33 |  | 
|  | 34 | // The device providing randomness. | 
|  | 35 | const char* kRandomDevice = "/dev/urandom"; | 
|  | 36 |  | 
|  | 37 | }  // namespace | 
|  | 38 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 39 | namespace chromeos_update_manager { | 
| Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 40 |  | 
| Gilad Arnold | 12db701 | 2014-01-29 16:45:30 -0800 | [diff] [blame] | 41 | // A random seed variable. | 
|  | 42 | class RandomSeedVariable : public Variable<uint64_t> { | 
| Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 43 | public: | 
| Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 44 | // RandomSeedVariable is initialized as kVariableModeConst to let the | 
|  | 45 | // EvaluationContext cache the value between different evaluations of the same | 
|  | 46 | // policy request. | 
| Gilad Arnold | 12db701 | 2014-01-29 16:45:30 -0800 | [diff] [blame] | 47 | RandomSeedVariable(const string& name, FILE* fp) | 
| Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 48 | : Variable<uint64_t>(name, kVariableModeConst), fp_(fp) {} | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 49 | ~RandomSeedVariable() override {} | 
| Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 50 |  | 
|  | 51 | protected: | 
| Yunlian Jiang | 35866ed | 2015-01-29 13:09:20 -0800 | [diff] [blame] | 52 | const uint64_t* GetValue(base::TimeDelta /* timeout */, | 
|  | 53 | string* errmsg) override { | 
| Gilad Arnold | 570ecb1 | 2014-01-29 10:52:29 -0800 | [diff] [blame] | 54 | uint64_t result; | 
|  | 55 | // Aliasing via char pointer abides by the C/C++ strict-aliasing rules. | 
|  | 56 | char* const buf = reinterpret_cast<char*>(&result); | 
| Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 57 | unsigned int buf_rd = 0; | 
|  | 58 |  | 
| Gilad Arnold | 570ecb1 | 2014-01-29 10:52:29 -0800 | [diff] [blame] | 59 | while (buf_rd < sizeof(result)) { | 
|  | 60 | 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] | 61 | if (rd == 0 || ferror(fp_.get())) { | 
|  | 62 | // Either EOF on fp or read failed. | 
| Gilad Arnold | 570ecb1 | 2014-01-29 10:52:29 -0800 | [diff] [blame] | 63 | if (errmsg) { | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 64 | *errmsg = base::StringPrintf( | 
|  | 65 | "Error reading from the random device: %s", kRandomDevice); | 
| Gilad Arnold | 570ecb1 | 2014-01-29 10:52:29 -0800 | [diff] [blame] | 66 | } | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 67 | return nullptr; | 
| Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 68 | } | 
|  | 69 | buf_rd += rd; | 
|  | 70 | } | 
| Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 71 |  | 
| Gilad Arnold | 570ecb1 | 2014-01-29 10:52:29 -0800 | [diff] [blame] | 72 | return new uint64_t(result); | 
| Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 73 | } | 
|  | 74 |  | 
|  | 75 | private: | 
| Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 76 | base::ScopedFILE fp_; | 
| Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 77 |  | 
|  | 78 | DISALLOW_COPY_AND_ASSIGN(RandomSeedVariable); | 
| Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 79 | }; | 
|  | 80 |  | 
| Alex Deymo | 42c30c3 | 2014-04-24 18:41:18 -0700 | [diff] [blame] | 81 | bool RealRandomProvider::Init(void) { | 
| Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 82 | FILE* fp = fopen(kRandomDevice, "r"); | 
|  | 83 | if (!fp) | 
|  | 84 | return false; | 
| David Zeuthen | 21716e2 | 2014-04-23 15:42:05 -0700 | [diff] [blame] | 85 | var_seed_.reset(new RandomSeedVariable("seed", fp)); | 
| Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 86 | return true; | 
|  | 87 | } | 
|  | 88 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 89 | }  // namespace chromeos_update_manager |