1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
/*
* Entropy source based on reading files in /proc on the assumption
* that a remote attacker will have difficulty guessing some of them.
*
* (C) 1999-2008,2012 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include <botan/internal/proc_walk.h>
#include <botan/secmem.h>
#include <deque>
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 199309
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
namespace Botan {
namespace {
class Directory_Walker : public File_Descriptor_Source
{
public:
Directory_Walker(const std::string& root) :
m_cur_dir(std::make_pair<DIR*, std::string>(nullptr, ""))
{
if(DIR* root_dir = ::opendir(root.c_str()))
m_cur_dir = std::make_pair(root_dir, root);
}
~Directory_Walker()
{
if(m_cur_dir.first)
::closedir(m_cur_dir.first);
}
int next_fd() override;
private:
std::pair<struct dirent*, std::string> get_next_dirent();
std::pair<DIR*, std::string> m_cur_dir;
std::deque<std::string> m_dirlist;
};
std::pair<struct dirent*, std::string> Directory_Walker::get_next_dirent()
{
while(m_cur_dir.first)
{
if(struct dirent* dir = ::readdir(m_cur_dir.first))
return std::make_pair(dir, m_cur_dir.second);
::closedir(m_cur_dir.first);
m_cur_dir = std::make_pair<DIR*, std::string>(nullptr, "");
while(!m_dirlist.empty() && !m_cur_dir.first)
{
const std::string next_dir_name = m_dirlist[0];
m_dirlist.pop_front();
if(DIR* next_dir = ::opendir(next_dir_name.c_str()))
m_cur_dir = std::make_pair(next_dir, next_dir_name);
}
}
return std::make_pair<struct dirent*, std::string>(nullptr, ""); // nothing left
}
int Directory_Walker::next_fd()
{
while(true)
{
std::pair<struct dirent*, std::string> entry = get_next_dirent();
if(!entry.first)
break; // no more dirs
const std::string filename = entry.first->d_name;
if(filename == "." || filename == "..")
continue;
const std::string full_path = entry.second + "/" + filename;
struct stat stat_buf;
if(::lstat(full_path.c_str(), &stat_buf) == -1)
continue;
if(S_ISDIR(stat_buf.st_mode))
{
m_dirlist.push_back(full_path);
}
else if(S_ISREG(stat_buf.st_mode) && (stat_buf.st_mode & S_IROTH))
{
int fd = ::open(full_path.c_str(), O_RDONLY | O_NOCTTY);
if(fd >= 0)
return fd;
}
}
return -1;
}
}
void ProcWalking_EntropySource::poll(Entropy_Accumulator& accum)
{
const size_t MAX_FILES_READ_PER_POLL = 2048;
const double ENTROPY_ESTIMATE = 1.0 / 128;
std::lock_guard<std::mutex> lock(m_mutex);
if(!m_dir)
m_dir.reset(new Directory_Walker(m_path));
m_buf.resize(4096);
for(size_t i = 0; i != MAX_FILES_READ_PER_POLL; ++i)
{
int fd = m_dir->next_fd();
// If we've exhaused this walk of the directory, halt the poll
if(fd == -1)
{
m_dir.reset();
break;
}
ssize_t got = ::read(fd, m_buf.data(), m_buf.size());
::close(fd);
if(got > 0)
accum.add(m_buf.data(), got, ENTROPY_ESTIMATE);
if(accum.polling_finished())
break;
}
}
}
|