aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/entropy/unix_procs/unix_procs.h
blob: 808d34221ef1b6d1631bee20a359a3f7af4dd2cd (plain)
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
/*
* Unix EntropySource
* (C) 1999-2009,2013 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#ifndef BOTAN_ENTROPY_SRC_UNIX_H__
#define BOTAN_ENTROPY_SRC_UNIX_H__

#include <botan/entropy_src.h>
#include <vector>
#include <mutex>

namespace Botan {

/**
* Entropy source for generic Unix. Runs various programs trying to
* gather data hard for a remote attacker to guess. Probably not too
* effective against local attackers as they can sample from the same
* distribution.
*/
class Unix_EntropySource : public EntropySource
   {
   public:
      std::string name() const override { return "Unix Process Runner"; }

      void poll(Entropy_Accumulator& accum) override;

      /**
      * @param trusted_paths is a list of directories that are assumed
      *        to contain only 'safe' binaries. If an attacker can write
      *        an executable to one of these directories then we will
      *        run arbitrary code.
      */
      Unix_EntropySource(const std::vector<std::string>& trusted_paths,
                         size_t concurrent_processes = 0);
   private:
      static std::vector<std::vector<std::string>> get_default_sources();

      class Unix_Process
         {
         public:
            int fd() const { return m_fd; }

            void spawn(const std::vector<std::string>& args);
            void shutdown();

            Unix_Process() {}

            Unix_Process(const std::vector<std::string>& args) { spawn(args); }

            ~Unix_Process() { shutdown(); }

            Unix_Process(Unix_Process&& other)
               {
               std::swap(m_fd, other.m_fd);
               std::swap(m_pid, other.m_pid);
               }

            Unix_Process(const Unix_Process&) = delete;
            Unix_Process& operator=(const Unix_Process&) = delete;
         private:
            int m_fd = -1;
            int m_pid = -1;
         };

      const std::vector<std::string>& next_source();

      std::mutex m_mutex;
      const std::vector<std::string> m_trusted_paths;
      const size_t m_concurrent;

      std::vector<std::vector<std::string>> m_sources;
      size_t m_sources_idx = 0;

      std::vector<Unix_Process> m_procs;
      secure_vector<byte> m_buf;
   };

class UnixProcessInfo_EntropySource : public EntropySource
   {
   public:
      std::string name() const override { return "Unix Process Info"; }

      void poll(Entropy_Accumulator& accum) override;
   };

}

#endif