Botan  1.11.15
src/lib/entropy/unix_procs/unix_procs.h
Go to the documentation of this file.
00001 /*
00002 * Unix EntropySource
00003 * (C) 1999-2009,2013 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #ifndef BOTAN_ENTROPY_SRC_UNIX_H__
00009 #define BOTAN_ENTROPY_SRC_UNIX_H__
00010 
00011 #include <botan/entropy_src.h>
00012 #include <vector>
00013 #include <mutex>
00014 
00015 namespace Botan {
00016 
00017 /**
00018 * Entropy source for generic Unix. Runs various programs trying to
00019 * gather data hard for a remote attacker to guess. Probably not too
00020 * effective against local attackers as they can sample from the same
00021 * distribution.
00022 */
00023 class Unix_EntropySource : public EntropySource
00024    {
00025    public:
00026       std::string name() const { return "Unix Process Runner"; }
00027 
00028       void poll(Entropy_Accumulator& accum) override;
00029 
00030       /**
00031       * @param trusted_paths is a list of directories that are assumed
00032       *        to contain only 'safe' binaries. If an attacker can write
00033       *        an executable to one of these directories then we will
00034       *        run arbitrary code.
00035       */
00036       Unix_EntropySource(const std::vector<std::string>& trusted_paths,
00037                          size_t concurrent_processes = 0);
00038    private:
00039       static std::vector<std::vector<std::string>> get_default_sources();
00040 
00041       class Unix_Process
00042          {
00043          public:
00044             int fd() const { return m_fd; }
00045 
00046             void spawn(const std::vector<std::string>& args);
00047             void shutdown();
00048 
00049             Unix_Process() {}
00050 
00051             Unix_Process(const std::vector<std::string>& args) { spawn(args); }
00052 
00053             ~Unix_Process() { shutdown(); }
00054 
00055             Unix_Process(Unix_Process&& other)
00056                {
00057                std::swap(m_fd, other.m_fd);
00058                std::swap(m_pid, other.m_pid);
00059                }
00060 
00061             Unix_Process(const Unix_Process&) = delete;
00062             Unix_Process& operator=(const Unix_Process&) = delete;
00063          private:
00064             int m_fd = -1;
00065             int m_pid = -1;
00066          };
00067 
00068       const std::vector<std::string>& next_source();
00069 
00070       std::mutex m_mutex;
00071       const std::vector<std::string> m_trusted_paths;
00072       const size_t m_concurrent;
00073 
00074       std::vector<std::vector<std::string>> m_sources;
00075       size_t m_sources_idx = 0;
00076 
00077       std::vector<Unix_Process> m_procs;
00078    };
00079 
00080 class UnixProcessInfo_EntropySource : public EntropySource
00081    {
00082    public:
00083       std::string name() const { return "Unix Process Info"; }
00084 
00085       void poll(Entropy_Accumulator& accum);
00086    };
00087 
00088 }
00089 
00090 #endif