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

Upstream all Fedora changes #65

Merged
merged 10 commits into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion Make.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ INSTALLROOT = $(DESTDIR)

INSTALL ?= install
CROSS_COMPILE ?=
EFI_ARCHES ?= aa64 ia32 x64

PKG_CONFIG ?= $(CROSS_COMPILE)pkg-config
CC := $(if $(filter default,$(origin CC)),$(CROSS_COMPILE)gcc,$(CC))
Expand All @@ -39,7 +40,7 @@ gcc_cflags = -Wmaybe-uninitialized -grecord-gcc-switches -flto
cflags = $(CFLAGS) $(ARCH3264) \
-Wall -Wextra -Wsign-compare -Wno-unused-result \
-Wno-unused-function -Wno-missing-field-initializers \
-Werror -Wno-error=cpp \
-Werror -Wno-error=cpp -Wno-free-nonheap-object \
-std=gnu11 -fshort-wchar -fPIC -fno-strict-aliasing \
-D_GNU_SOURCE -DCONFIG_$(ARCH) -I${TOPDIR}/include \
$(if $(filter $(CC),clang),$(clang_cflags), ) \
Expand Down
10 changes: 8 additions & 2 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ include $(TOPDIR)/Make.version
include $(TOPDIR)/Make.rules
include $(TOPDIR)/Make.defaults

BINTARGETS=authvar client efikeygen efisiglist pesigcheck pesign
BINTARGETS=authvar client efikeygen efisiglist pesigcheck pesign pesign-rpmbuild-helper
SVCTARGETS=pesign.sysvinit pesign.service
TARGETS=$(BINTARGETS) $(SVCTARGETS)

Expand Down Expand Up @@ -49,6 +49,11 @@ pesign : $(call objects-of,$(PESIGN_SOURCES) $(COMMON_SOURCES) $(COMMON_PE_SOURC
pesign : LDLIBS+=$(TOPDIR)/libdpe/libdpe.a
pesign : PKGS=efivar nss nspr popt

pesign-rpmbuild-helper: pesign-rpmbuild-helper.in
sed \
-e "s/@@EFI_ARCHES@@/$(EFI_ARCHES)/g" \
$^ > $@

deps : PKGS=efivar nss nspr popt uuid
deps : $(ALL_SOURCES)
$(MAKE) -f $(TOPDIR)/Make.deps \
Expand All @@ -73,7 +78,7 @@ install_sysvinit: pesign.sysvinit
install :
$(INSTALL) -d -m 700 $(INSTALLROOT)/etc/pki/pesign/
$(INSTALL) -d -m 700 $(INSTALLROOT)/etc/pki/pesign-rh-test/
$(INSTALL) -d -m 770 $(INSTALLROOT)/var/run/pesign/
$(INSTALL) -d -m 770 $(INSTALLROOT)/run/pesign/
$(INSTALL) -d -m 755 $(INSTALLROOT)$(bindir)
$(INSTALL) -m 755 authvar $(INSTALLROOT)$(bindir)
$(INSTALL) -m 755 pesign $(INSTALLROOT)$(bindir)
Expand All @@ -94,6 +99,7 @@ install :
$(INSTALL) -m 644 macros.pesign $(INSTALLROOT)/etc/rpm/
$(INSTALL) -d -m 755 $(INSTALLROOT)$(libexecdir)/pesign/
$(INSTALL) -m 750 pesign-authorize $(INSTALLROOT)$(libexecdir)/pesign/
$(INSTALL) -m 755 pesign-rpmbuild-helper $(INSTALLROOT)$(libexecdir)/pesign/
$(INSTALL) -d -m 700 $(INSTALLROOT)/etc/pesign
$(INSTALL) -m 600 pesign-users $(INSTALLROOT)/etc/pesign/users
$(INSTALL) -m 600 pesign-groups $(INSTALLROOT)/etc/pesign/groups
Expand Down
112 changes: 80 additions & 32 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <fcntl.h>
#include <popt.h>
#include <pwd.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <sys/socket.h>
Expand Down Expand Up @@ -60,44 +61,62 @@ print_flag_name(FILE *f, int flag)
}

static int
connect_to_server(void)
connect_to_server_helper(const char * const sockpath)
{
int rc = access(SOCKPATH, R_OK);
int rc = access(sockpath, R_OK);
if (rc != 0) {
fprintf(stderr, "pesign-client: could not connect to server: "
"%m\n");
exit(1);
warn("could not access socket \"%s\"", sockpath);
return rc;
}

struct sockaddr_un addr_un = {
.sun_family = AF_UNIX,
.sun_path = SOCKPATH,
};
strncpy(addr_un.sun_path, sockpath, sizeof(addr_un.sun_path));
addr_un.sun_path[sizeof(addr_un.sun_path)-1] = '\0';

int sd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sd < 0) {
fprintf(stderr, "pesign-client: could not open socket: %m\n");
exit(1);
warn("could not open socket \"%s\"", sockpath);
return sd;
}

socklen_t len = strlen(addr_un.sun_path) +
sizeof(addr_un.sun_family);

rc = connect(sd, (struct sockaddr *)&addr_un, len);
if (rc < 0) {
fprintf(stderr, "pesign-client: could not connect to daemon: "
"%m\n");
exit(1);
warn("could not connect to daemon");
return sd;
}

return sd;
}

static int
connect_to_server(void)
{
int rc, i;
const char * const sockets[] = {
"/run/pesign/socket",
"/var/run/pesign/socket",
NULL
};

for (i = 0; sockets[i] != NULL; i++) {
rc = connect_to_server_helper(sockets[i]);
if (rc >= 0)
return rc;
}

exit(1);
}

static int32_t
check_response(int sd, char **srvmsg);

static void
check_cmd_version(int sd, uint32_t command, char *name, int32_t version)
static int
check_cmd_version(int sd, uint32_t command, char *name, int32_t version, bool do_exit)
{
struct msghdr msg;
struct iovec iov[1];
Expand All @@ -116,7 +135,7 @@ check_cmd_version(int sd, uint32_t command, char *name, int32_t version)
ssize_t n;
n = sendmsg(sd, &msg, 0);
if (n < 0) {
fprintf(stderr, "check-cmd-version: kill daemon failed: %m\n");
fprintf(stderr, "check-cmd-version: sendmsg failed: %m\n");
exit(1);
}

Expand All @@ -132,11 +151,16 @@ check_cmd_version(int sd, uint32_t command, char *name, int32_t version)

char *srvmsg = NULL;
int32_t rc = check_response(sd, &srvmsg);
if (rc < 0)

if (do_exit && rc < 0)
errx(1, "command \"%s\" not known by server", name);
if (rc != version)
if (do_exit && rc != version)
errx(1, "command \"%s\": client version %#x, server version %#x",
name, version, rc);

if (rc < 0)
return rc;
return rc == version;
}

static void
Expand All @@ -146,7 +170,7 @@ send_kill_daemon(int sd)
struct iovec iov;
pesignd_msghdr pm;

check_cmd_version(sd, CMD_KILL_DAEMON, "kill-daemon", 0);
check_cmd_version(sd, CMD_KILL_DAEMON, "kill-daemon", 0, true);

pm.version = PESIGND_VERSION;
pm.command = CMD_KILL_DAEMON;
Expand Down Expand Up @@ -288,7 +312,7 @@ unlock_token(int sd, char *tokenname, char *pin)

uint32_t size1 = pesignd_string_size(pin);

check_cmd_version(sd, CMD_UNLOCK_TOKEN, "unlock-token", 0);
check_cmd_version(sd, CMD_UNLOCK_TOKEN, "unlock-token", 0, true);

pm.version = PESIGND_VERSION;
pm.command = CMD_UNLOCK_TOKEN;
Expand Down Expand Up @@ -365,7 +389,7 @@ is_token_unlocked(int sd, char *tokenname)

uint32_t size0 = pesignd_string_size(tokenname);

check_cmd_version(sd, CMD_IS_TOKEN_UNLOCKED, "is-token-unlocked", 0);
check_cmd_version(sd, CMD_IS_TOKEN_UNLOCKED, "is-token-unlocked", 0, true);

pm.version = PESIGND_VERSION;
pm.command = CMD_IS_TOKEN_UNLOCKED;
Expand Down Expand Up @@ -464,6 +488,9 @@ static void
sign(int sd, char *infile, char *outfile, char *tokenname, char *certname,
int attached, uint32_t format)
{
int rc;
bool add_file_type;

int infd = open(infile, O_RDONLY);
if (infd < 0) {
fprintf(stderr, "pesign-client: could not open input file "
Expand Down Expand Up @@ -493,12 +520,27 @@ sign(int sd, char *infile, char *outfile, char *tokenname, char *certname,
exit(1);
}

check_cmd_version(sd, attached ? CMD_SIGN_ATTACHED : CMD_SIGN_DETACHED,
attached ? "sign-attached" : "sign-detached", 0);
rc = check_cmd_version(sd,
attached ? CMD_SIGN_ATTACHED_WITH_FILE_TYPE
: CMD_SIGN_DETACHED_WITH_FILE_TYPE,
attached ? "sign-attached" : "sign-detached",
0, format == FORMAT_KERNEL_MODULE);
if (rc >= 0) {
add_file_type = true;
} else {
add_file_type = false;
check_cmd_version(sd, attached ? CMD_SIGN_ATTACHED
: CMD_SIGN_DETACHED,
attached ? "sign-attached" : "sign-detached",
0, true);
}

pm->version = PESIGND_VERSION;
pm->command = attached ? CMD_SIGN_ATTACHED : CMD_SIGN_DETACHED;
pm->size = size0 + size1 + sizeof(format);
pm->command = attached ? (add_file_type ? CMD_SIGN_ATTACHED_WITH_FILE_TYPE
: CMD_SIGN_ATTACHED)
: (add_file_type ? CMD_SIGN_DETACHED_WITH_FILE_TYPE
: CMD_SIGN_DETACHED);
pm->size = size0 + size1 + (add_file_type ? sizeof(format) : 0);
iov[0].iov_base = pm;
iov[0].iov_len = sizeof (*pm);

Expand All @@ -515,25 +557,31 @@ sign(int sd, char *infile, char *outfile, char *tokenname, char *certname,
}

char *buffer;
buffer = malloc(size0 + size1);
buffer = malloc(pm->size);
if (!buffer)
goto oom;

iov[0].iov_base = &format;
iov[0].iov_len = sizeof(format);
int pos = 0;

if (add_file_type) {
iov[pos].iov_base = &format;
iov[pos].iov_len = sizeof(format);
pos++;
}

pesignd_string *tn = (pesignd_string *)buffer;
pesignd_string_set(tn, tokenname);
iov[1].iov_base = tn;
iov[1].iov_len = size0;
iov[pos].iov_base = tn;
iov[pos].iov_len = size0;
pos++;

pesignd_string *cn = pesignd_string_next(tn);
pesignd_string_set(cn, certname);
iov[2].iov_base = cn;
iov[2].iov_len = size1;
iov[pos].iov_base = cn;
iov[pos].iov_len = size1;

msg.msg_iov = iov;
msg.msg_iovlen = 3;
msg.msg_iovlen = add_file_type ? 3 : 2;

n = sendmsg(sd, &msg, 0);
if (n < 0) {
Expand All @@ -547,7 +595,7 @@ sign(int sd, char *infile, char *outfile, char *tokenname, char *certname,
send_fd(sd, outfd);

char *srvmsg = NULL;
int rc = check_response(sd, &srvmsg);
rc = check_response(sd, &srvmsg);
if (rc < 0) {
fprintf(stderr, "pesign-client: signing failed: \"%s\"\n",
srvmsg);
Expand Down
49 changes: 35 additions & 14 deletions src/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <poll.h>
#include <pwd.h>
#include <signal.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -569,7 +570,7 @@ sign_kmod(context *ctx, int infd, int outfd, int attached)

static void
handle_signing(context *ctx, struct pollfd *pollfd, socklen_t size,
int attached)
int attached, bool with_file_type)
{
struct msghdr msg;
struct iovec iov;
Expand All @@ -593,8 +594,12 @@ handle_signing(context *ctx, struct pollfd *pollfd, socklen_t size,

n = recvmsg(pollfd->fd, &msg, MSG_WAITALL);

file_format = *((uint32_t *) buffer);
n -= sizeof(uint32_t);
if (with_file_type) {
file_format = *((uint32_t *) buffer);
n -= sizeof(uint32_t);
} else {
file_format = FORMAT_PE_BINARY;
}

pesignd_string *tn = (pesignd_string *)(buffer + sizeof(uint32_t));
if (n < (long long)sizeof(tn->size)) {
Expand Down Expand Up @@ -674,34 +679,44 @@ handle_signing(context *ctx, struct pollfd *pollfd, socklen_t size,
teardown_digests(ctx->cms);
}

static void
handle_sign_attached(context *ctx, struct pollfd *pollfd, socklen_t size)
static inline void
handle_sign_helper(context *ctx, struct pollfd *pollfd, socklen_t size,
int attached, bool with_file_type)
{
int rc = cms_context_alloc(&ctx->cms);
if (rc < 0)
return;

steal_from_cms(ctx->backup_cms, ctx->cms);

handle_signing(ctx, pollfd, size, 1);
handle_signing(ctx, pollfd, size, attached, with_file_type);

hide_stolen_goods_from_cms(ctx->cms, ctx->backup_cms);
cms_context_fini(ctx->cms);
}

static void
handle_sign_detached(context *ctx, struct pollfd *pollfd, socklen_t size)
handle_sign_attached(context *ctx, struct pollfd *pollfd, socklen_t size)
{
int rc = cms_context_alloc(&ctx->cms);
if (rc < 0)
return;
handle_sign_helper(ctx, pollfd, size, 1, false);
}

steal_from_cms(ctx->backup_cms, ctx->cms);
static void
handle_sign_attached_with_file_type(context *ctx, struct pollfd *pollfd, socklen_t size)
{
handle_sign_helper(ctx, pollfd, size, 1, true);
}

handle_signing(ctx, pollfd, size, 0);
static void
handle_sign_detached(context *ctx, struct pollfd *pollfd, socklen_t size)
{
handle_sign_helper(ctx, pollfd, size, 0, false);
}

hide_stolen_goods_from_cms(ctx->cms, ctx->backup_cms);
cms_context_fini(ctx->cms);
static void
handle_sign_detached_with_file_type(context *ctx, struct pollfd *pollfd, socklen_t size)
{
handle_sign_helper(ctx, pollfd, size, 0, true);
}

static void
Expand Down Expand Up @@ -733,6 +748,12 @@ cmd_table_t cmd_table[] = {
{ CMD_UNLOCK_TOKEN, handle_unlock_token, "unlock-token", 0 },
{ CMD_SIGN_ATTACHED, handle_sign_attached, "sign-attached", 0 },
{ CMD_SIGN_DETACHED, handle_sign_detached, "sign-detached", 0 },
{ CMD_SIGN_ATTACHED_WITH_FILE_TYPE,
handle_sign_attached_with_file_type,
"sign-attached-with-file-type", 0 },
{ CMD_SIGN_DETACHED_WITH_FILE_TYPE,
handle_sign_detached_with_file_type,
"sign-detached-with-file-type", 0 },
{ CMD_RESPONSE, NULL, "response", 0 },
{ CMD_IS_TOKEN_UNLOCKED, handle_is_token_unlocked,
"is-token-unlocked", 0 },
Expand Down
Loading