USBGuard
Software framework that protects your computer against rogue USB devices by implementing basic whitelisting and blacklisting capabilities.
Audit.hpp
1 //
2 // Copyright (C) 2017 Red Hat, Inc.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // Authors: Daniel Kopecek <dkopecek@redhat.com>
18 //
19 #pragma once
20 
21 #include "Policy.hpp"
22 
23 #include "Device.hpp"
24 #include "DeviceManager.hpp"
25 #include "Logger.hpp"
26 #include "Rule.hpp"
27 #include "Typedefs.hpp"
28 
29 #include <string>
30 #include <memory>
31 #include <mutex>
32 #include <unordered_map>
33 
34 #include <unistd.h>
35 #include <sys/types.h>
36 
37 namespace usbguard
38 {
42  class DLL_PUBLIC AuditIdentity
43  {
44  public:
49  AuditIdentity();
50 
57  AuditIdentity(uid_t uid, pid_t pid);
58 
64  uid_t uid() const;
65 
71  pid_t pid() const;
72 
80  std::string toString() const;
81 
82  private:
83  uid_t _uid;
84  pid_t _pid;
85  };
86 
87  class AuditBackend;
88 
93  class DLL_PUBLIC AuditEvent
94  {
105  AuditEvent(const AuditIdentity& identity, std::shared_ptr<AuditBackend>& backend);
106  public:
107  using Keys = std::unordered_map<std::string, std::string>;
108 
114  AuditEvent(AuditEvent&& event);
115 
116  AuditEvent(const AuditEvent& event) = delete;
117 
125  ~AuditEvent();
126 
130  void success();
131 
135  void failure();
136 
142  const AuditIdentity& identity() const;
143 
150  const Keys& keys() const;
151 
152  private:
163  void commit(const std::string& result);
164 
171  void setCommited(bool state);
172 
179  void setKey(const std::string& key, const std::string& value);
180 
181  bool _commited;
183  AuditIdentity _identity;
184  std::shared_ptr<AuditBackend> _backend;
186  Keys _keys;
189  friend class Audit;
190  };
191 
195  class DLL_PUBLIC AuditBackend
196  {
197  public:
201  AuditBackend();
202 
206  virtual ~AuditBackend();
207 
214  virtual void write(const AuditEvent& event) = 0;
215 
224  void commit(const AuditEvent& event);
225 
226  private:
227  std::mutex _mutex;
228  };
229 
233  class DLL_PUBLIC Audit
234  {
235  public:
242  Audit(const AuditIdentity& identity);
243 
251  void setBackend(std::unique_ptr<AuditBackend> backend);
252 
261  void setHidePII(bool hide_pii);
262 
288  AuditEvent policyEvent(std::shared_ptr<Rule> rule, Policy::EventType event);
289 
316  AuditEvent policyEvent(std::shared_ptr<Rule> new_rule, std::shared_ptr<Rule> old_rule);
317 
344  AuditEvent policyEvent(std::shared_ptr<Device> device, Policy::EventType event);
345 
374  AuditEvent policyEvent(std::shared_ptr<Device> device, Rule::Target old_target, Rule::Target new_target);
375 
400  AuditEvent deviceEvent(std::shared_ptr<Device> device, DeviceManager::EventType event);
401 
427  AuditEvent deviceEvent(std::shared_ptr<Device> new_device, std::shared_ptr<Device> old_device);
428 
455  AuditEvent policyEvent(const AuditIdentity& identity, std::shared_ptr<Rule> rule, Policy::EventType event);
456 
484  AuditEvent policyEvent(const AuditIdentity& identity, std::shared_ptr<Rule> new_rule, std::shared_ptr<Rule> old_rule);
485 
513  AuditEvent policyEvent(const AuditIdentity& identity, std::shared_ptr<Device> device, Policy::EventType event);
514 
544  AuditEvent policyEvent(const AuditIdentity& identity, std::shared_ptr<Device> device, Rule::Target old_target,
545  Rule::Target new_target);
546 
572  AuditEvent deviceEvent(const AuditIdentity& identity, std::shared_ptr<Device> device, DeviceManager::EventType event);
573 
600  AuditEvent deviceEvent(const AuditIdentity& identity, std::shared_ptr<Device> new_device, std::shared_ptr<Device> old_device);
601 
602  private:
603  AuditIdentity _identity;
604  std::shared_ptr<AuditBackend> _backend;
608  bool _hide_pii;
612  };
613 } /* namespace usbguard */
614 
615 /* vim: set ts=2 sw=2 et */
Describes policy or device event and its result that should be audited using given audit identity and...
Definition: Audit.hpp:93
Target
Enumeration of possible rule targets.
Definition: Rule.hpp:86
Represents the audit identity.
Definition: Audit.hpp:42
Commits and writes audit events into log.
Definition: Audit.hpp:195
Generates audit events for given policy or device events.
Definition: Audit.hpp:233
EventType
Type of event that took place on the device.
Definition: DeviceManager.hpp:51