Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove code guarded by #ifdef kernel macros #752

Merged
merged 3 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Source/common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ load("@rules_proto_grpc//objc:defs.bzl", "objc_proto_library")

package(
default_visibility = ["//:santa_package_group"],
features = ["-layering_check"],
features = ["layering_check"],
)

licenses(["notice"])
Expand Down
30 changes: 3 additions & 27 deletions Source/common/SNTCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,16 @@
/// Common defines between kernel <-> userspace
///

tnek marked this conversation as resolved.
Show resolved Hide resolved
#ifndef SANTA__COMMON__KERNELCOMMON_H
#define SANTA__COMMON__KERNELCOMMON_H
#ifndef SANTA__COMMON__COMMON_H
#define SANTA__COMMON__COMMON_H

#include <stdint.h>
#include <sys/param.h>

// Defines the name of the userclient class and the driver bundle ID.
#define USERCLIENT_CLASS "com_google_SantaDriver"
#define USERCLIENT_ID "com.google.santa-driver"

// Branch prediction
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)

// List of methods supported by the driver.
enum SantaDriverMethods {
kSantaUserClientOpen,
kSantaUserClientAllowBinary,
kSantaUserClientAllowCompiler,
kSantaUserClientDenyBinary,
kSantaUserClientAcknowledgeBinary,
kSantaUserClientClearCache,
kSantaUserClientRemoveCacheEntry,
kSantaUserClientCacheCount,
kSantaUserClientCheckCache,
kSantaUserClientCacheBucketCount,
kSantaUserClientFilemodPrefixFilterAdd,
kSantaUserClientFilemodPrefixFilterReset,

// Any methods supported by the driver should be added above this line to
// ensure this remains the count of methods.
kSantaUserClientNMethods,
};

tnek marked this conversation as resolved.
Show resolved Hide resolved
typedef enum {
QUEUETYPE_DECISION,
QUEUETYPE_LOG,
Expand Down Expand Up @@ -143,4 +119,4 @@ typedef struct {
uint64_t start;
} santa_bucket_count_t;

#endif // SANTA__COMMON__KERNELCOMMON_H
tnek marked this conversation as resolved.
Show resolved Hide resolved
#endif // SANTA__COMMON__COMMON_H
1 change: 0 additions & 1 deletion Source/common/SNTCommonEnums.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ typedef NS_ENUM(NSInteger, SNTMetricFormatType) {
SNTMetricFormatTypeMonarchJSON,
};

static const char *kKextPath = "/Library/Extensions/santa-driver.kext";
static const char *kSantaDPath =
"/Applications/Santa.app/Contents/Library/SystemExtensions/"
"com.google.santa.daemon.systemextension/Contents/MacOS/com.google.santa.daemon";
Expand Down
17 changes: 0 additions & 17 deletions Source/common/SNTLogging.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,6 @@
#ifndef SANTA__COMMON__LOGGING_H
tnek marked this conversation as resolved.
Show resolved Hide resolved
#define SANTA__COMMON__LOGGING_H

#ifdef KERNEL

#include <IOKit/IOLib.h>

#ifdef DEBUG
#define LOGD(format, ...) IOLog("D santa-driver: " format "\n", ##__VA_ARGS__);
#else // DEBUG
#define LOGD(format, ...)
#endif // DEBUG
#define LOGI(format, ...) IOLog("I santa-driver: " format "\n", ##__VA_ARGS__);
#define LOGW(format, ...) IOLog("W santa-driver: " format "\n", ##__VA_ARGS__);
#define LOGE(format, ...) IOLog("E santa-driver: " format "\n", ##__VA_ARGS__);

#else // KERNEL

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -68,6 +53,4 @@ void logMessage(LogLevel level, FILE *destination, NSString *format, ...)
} // extern C
#endif

#endif // KERNEL

#endif // SANTA__COMMON__LOGGING_H
45 changes: 0 additions & 45 deletions Source/common/SNTPrefixTree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@

#include "Source/common/SNTPrefixTree.h"

#ifdef KERNEL
#include <libkern/locks.h>

#include "Source/common/SNTLogging.h"

#else

#include <string.h>

#include <mutex>
Expand All @@ -46,25 +39,14 @@

#define lck_mtx_lock(l) l->lock()
#define lck_mtx_unlock(l) l->unlock()
#endif // KERNEL

SNTPrefixTree::SNTPrefixTree(uint32_t max_nodes) {
root_ = new SantaPrefixNode();
node_count_ = 0;
max_nodes_ = max_nodes;

#ifdef KERNEL
spt_lock_grp_attr_ = lck_grp_attr_alloc_init();
spt_lock_grp_ =
lck_grp_alloc_init("santa-prefix-tree-lock", spt_lock_grp_attr_);
spt_lock_attr_ = lck_attr_alloc_init();

spt_lock_ = lck_rw_alloc_init(spt_lock_grp_, spt_lock_attr_);
spt_add_lock_ = lck_mtx_alloc_init(spt_lock_grp_, spt_lock_attr_);
#else
pthread_rwlock_init(&spt_lock_, nullptr);
spt_add_lock_ = new std::mutex;
#endif
}

IOReturn SNTPrefixTree::AddPrefix(const char *prefix, uint64_t *node_count) {
Expand Down Expand Up @@ -241,32 +223,5 @@ SNTPrefixTree::~SNTPrefixTree() {
root_ = nullptr;
lck_rw_unlock_exclusive(spt_lock_);

#ifdef KERNEL
if (spt_lock_) {
lck_rw_free(spt_lock_, spt_lock_grp_);
spt_lock_ = nullptr;
}

if (spt_add_lock_) {
lck_mtx_free(spt_add_lock_, spt_lock_grp_);
spt_add_lock_ = nullptr;
}

if (spt_lock_attr_) {
lck_attr_free(spt_lock_attr_);
spt_lock_attr_ = nullptr;
}

if (spt_lock_grp_) {
lck_grp_free(spt_lock_grp_);
spt_lock_grp_ = nullptr;
}

if (spt_lock_grp_attr_) {
lck_grp_attr_free(spt_lock_grp_attr_);
spt_lock_grp_attr_ = nullptr;
}
#else
pthread_rwlock_destroy(&spt_lock_);
#endif
}
12 changes: 0 additions & 12 deletions Source/common/SNTPrefixTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@
#include <IOKit/IOReturn.h>
#include <sys/param.h>

#ifdef KERNEL
#include <libkern/locks.h>
#else
// Support for unit testing.
#include <pthread.h>
#include <stdint.h>

#include <mutex>
#endif // KERNEL

///
/// SantaPrefixTree is a simple prefix tree implementation.
Expand Down Expand Up @@ -88,16 +84,8 @@ class SNTPrefixTree {
uint32_t max_nodes_;
uint32_t node_count_;

#ifdef KERNEL
lck_grp_t *spt_lock_grp_;
lck_grp_attr_t *spt_lock_grp_attr_;
lck_attr_t *spt_lock_attr_;
lck_rw_t *spt_lock_;
lck_mtx_t *spt_add_lock_;
#else // KERNEL
pthread_rwlock_t spt_lock_;
std::mutex *spt_add_lock_;
#endif // KERNEL
};

#endif /* SANTA__SANTA_DRIVER__SANTAPREFIXTREE_H */
6 changes: 0 additions & 6 deletions Source/common/SantaCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@

#include "Source/common/SNTCommon.h"

#ifdef KERNEL
#include <IOKit/IOLib.h>
#else // KERNEL
// Support for unit testing.
tnek marked this conversation as resolved.
Show resolved Hide resolved
#include <cstdio>
#include <cstdlib>
Expand All @@ -41,7 +38,6 @@
#define OSDecrementAtomic(addr) OSAtomicDecrement64((volatile int64_t *)addr)
tnek marked this conversation as resolved.
Show resolved Hide resolved
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#endif // KERNEL

/**
A type to specialize to help SantaCache with its hashing.
Expand Down Expand Up @@ -375,8 +371,6 @@ class SantaCache {
}
};

#ifndef KERNEL
#pragma clang diagnostic pop
#endif

#endif // SANTA__SANTA_DRIVER__SANTACACHE_H
2 changes: 1 addition & 1 deletion Source/santactl/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ licenses(["notice"])

package(
default_visibility = ["//:santa_package_group"],
features = ["-layering_check"],
features = ["layering_check"],
)

objc_library(
Expand Down