2017-12-19 00:04:06 +00:00
|
|
|
/**
|
2016-02-11 19:48:58 +00:00
|
|
|
* Copyright (c) 2014-present, Facebook, Inc.
|
2015-06-30 21:16:43 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2017-12-19 00:04:06 +00:00
|
|
|
* This source code is licensed under both the Apache 2.0 license (found in the
|
|
|
|
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
|
|
|
|
* in the COPYING file in the root directory of this source tree).
|
|
|
|
* You may select, at your option, one of the above-listed licenses.
|
2015-06-30 21:16:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2015-08-16 03:43:53 +00:00
|
|
|
#include <vector>
|
2015-06-30 21:16:43 +00:00
|
|
|
|
|
|
|
#include <osquery/events.h>
|
|
|
|
#include <osquery/status.h>
|
|
|
|
|
2015-08-16 03:43:53 +00:00
|
|
|
#include "osquery/events/kernel/circular_queue_user.h"
|
2015-07-27 19:11:44 +00:00
|
|
|
|
2015-06-30 21:16:43 +00:00
|
|
|
namespace osquery {
|
|
|
|
|
2015-08-16 03:43:53 +00:00
|
|
|
/**
|
|
|
|
* @brief Name of the kernel communication device node.
|
|
|
|
*
|
|
|
|
* The kernel component creates an ioctl API for synchronizing kernel-based
|
|
|
|
* subscriptions and userland access to regions of shared memory.
|
|
|
|
*/
|
|
|
|
extern const std::string kKernelDevice;
|
|
|
|
|
2015-07-29 01:38:48 +00:00
|
|
|
/**
|
|
|
|
* @brief Load kernel extension if applicable.
|
|
|
|
*/
|
|
|
|
void loadKernelExtension();
|
|
|
|
|
2015-06-30 21:16:43 +00:00
|
|
|
/**
|
|
|
|
* @brief Subscription details for KernelEventPublisher events.
|
|
|
|
*/
|
|
|
|
struct KernelSubscriptionContext : public SubscriptionContext {
|
|
|
|
/// The kernel event subscription type.
|
|
|
|
osquery_event_t event_type;
|
2015-07-13 21:38:49 +00:00
|
|
|
|
2015-12-08 06:06:32 +00:00
|
|
|
/// Optional category passed to the callback.
|
|
|
|
std::string category;
|
2015-06-30 21:16:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Event details for a KernelEventPubliser events.
|
|
|
|
*/
|
|
|
|
struct KernelEventContext : public EventContext {
|
|
|
|
/// The event type.
|
|
|
|
osquery_event_t event_type;
|
2015-06-30 21:20:04 +00:00
|
|
|
|
2015-12-08 06:06:32 +00:00
|
|
|
/// The observed uptime of the system at event time.
|
2015-12-08 01:15:30 +00:00
|
|
|
uint32_t uptime{0};
|
2015-06-30 21:16:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename EventType>
|
|
|
|
struct TypedKernelEventContext : public KernelEventContext {
|
|
|
|
EventType event;
|
2015-12-08 01:15:30 +00:00
|
|
|
|
|
|
|
// The flexible data must remain as the last member.
|
2015-07-27 19:11:44 +00:00
|
|
|
std::vector<char> flexible_data;
|
2015-06-30 21:16:43 +00:00
|
|
|
};
|
|
|
|
|
2015-12-08 01:15:30 +00:00
|
|
|
using KernelSubscriptionContextRef = std::shared_ptr<KernelSubscriptionContext>;
|
|
|
|
using KernelEventContextRef = std::shared_ptr<KernelEventContext>;
|
|
|
|
|
2015-06-30 21:16:43 +00:00
|
|
|
template <typename EventType>
|
|
|
|
using TypedKernelEventContextRef =
|
2016-05-11 18:47:42 +00:00
|
|
|
std::shared_ptr<TypedKernelEventContext<EventType>>;
|
2015-06-30 21:16:43 +00:00
|
|
|
|
|
|
|
class KernelEventPublisher
|
|
|
|
: public EventPublisher<KernelSubscriptionContext, KernelEventContext> {
|
|
|
|
DECLARE_PUBLISHER("kernel");
|
|
|
|
|
|
|
|
public:
|
2016-05-11 18:47:42 +00:00
|
|
|
KernelEventPublisher() : EventPublisher(), queue_(nullptr) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Attempt to load the platform's kernel component.
|
|
|
|
*
|
|
|
|
* This method starts the kernel event publisher. As a convenience, the daemon
|
|
|
|
* will try to start/add/load the kernel component. For example on OS X this
|
|
|
|
* will load the osquery kernel extension if available. Each platform should
|
|
|
|
* include a `osquery::loadKernelExtension` method to perform the load.
|
|
|
|
*
|
|
|
|
* The osquery kernel component expects to make a queue available to the
|
|
|
|
* daemon, so the setup method will attempt to connect and request
|
|
|
|
* initialization of the queue, see the `osquery::CQueue` APIs.
|
|
|
|
*
|
|
|
|
* This should return a failure status if the queue cannot be initialized.
|
|
|
|
*/
|
2015-12-08 01:15:30 +00:00
|
|
|
Status setUp() override;
|
|
|
|
|
2016-05-11 18:47:42 +00:00
|
|
|
/**
|
|
|
|
* @brief Translate event subscribers into kernel subscriptions.
|
|
|
|
*
|
|
|
|
* The kernel component also uses a subscription abstraction. We expect to
|
|
|
|
* register kernel-based callbacks or start kernel threads that publish into
|
|
|
|
* a circular queue. When the queue is initialized it may communicate to each
|
|
|
|
* of these kernel publishers.
|
|
|
|
*/
|
2015-12-08 01:15:30 +00:00
|
|
|
void configure() override;
|
2015-06-30 21:16:43 +00:00
|
|
|
|
2016-05-11 18:47:42 +00:00
|
|
|
void stop() override;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Remove the circular queue.
|
|
|
|
*/
|
|
|
|
void tearDown() override { stop(); }
|
2015-06-30 21:16:43 +00:00
|
|
|
|
2016-05-11 18:47:42 +00:00
|
|
|
/**
|
|
|
|
* @brief Continue to flush the queue.
|
|
|
|
*/
|
2015-12-08 01:15:30 +00:00
|
|
|
Status run() override;
|
2015-06-30 21:16:43 +00:00
|
|
|
|
|
|
|
private:
|
2016-05-11 18:47:42 +00:00
|
|
|
/// Queue access mutex.
|
|
|
|
Mutex mutex_;
|
|
|
|
|
2015-12-08 01:15:30 +00:00
|
|
|
CQueue *queue_{nullptr};
|
2015-06-30 21:16:43 +00:00
|
|
|
|
|
|
|
/// Check whether the subscription matches the event.
|
|
|
|
bool shouldFire(const KernelSubscriptionContextRef &sc,
|
2015-12-08 01:15:30 +00:00
|
|
|
const KernelEventContextRef &ec) const override;
|
2015-06-30 21:16:43 +00:00
|
|
|
|
|
|
|
template <typename EventType>
|
|
|
|
KernelEventContextRef createEventContextFrom(osquery_event_t event_type,
|
2015-06-30 21:20:04 +00:00
|
|
|
CQueue::event *event) const;
|
2015-06-30 21:16:43 +00:00
|
|
|
};
|
|
|
|
|
2015-12-08 01:15:30 +00:00
|
|
|
} // namespace osquery
|