Skip to content

Commit

Permalink
pivy-agent: add zone info to logs, also add info logs about ecdh ops
Browse files Browse the repository at this point in the history
  • Loading branch information
arekinath committed Jun 17, 2024
1 parent b8ec179 commit 5d54eee
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
12 changes: 12 additions & 0 deletions bunyan.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
#include <fcntl.h>
#include <inttypes.h>


#include "debug.h"

#include "bunyan.h"
#include "errf.h"
#include "utils.h"

#include "openssh/sshkey.h"

static const char *bunyan_name = NULL;

/*
Expand Down Expand Up @@ -470,6 +473,7 @@ bunyan_log(enum bunyan_log_level level, const char *msg, ...)
uint uintval;
uint64_t uint64val;
size_t szval;
struct sshkey *pubk;

propname = va_arg(ap, const char *);
if (propname == NULL)
Expand Down Expand Up @@ -524,6 +528,14 @@ bunyan_log(enum bunyan_log_level level, const char *msg, ...)
evar->bv_next = evars;
evars = evar;
break;
case BNY_SSHKEY:
pubk = va_arg(ap, struct sshkey *);
wstrval = sshkey_fingerprint(pubk, SSH_DIGEST_SHA256,
SSH_FP_BASE64);
printf_buf("%s = %s key (%u bits): %s", propname,
sshkey_type(pubk), sshkey_size(pubk), wstrval);
free(wstrval);
break;
default:
abort();
}
Expand Down
1 change: 1 addition & 0 deletions bunyan.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ enum bunyan_arg_type {
BNY_SIZE_T,
BNY_BIN_HEX,
BNY_ERF,
BNY_SSHKEY,
};

void bunyan_init(void);
Expand Down
33 changes: 29 additions & 4 deletions pivy-agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ typedef struct socket_entry {
pid_entry_t *se_pid_ent;
uint se_pid_idx;
sessbind_t se_sbind;
#if defined(__sun)
zoneid_t se_zid;
char se_zname[128];
#endif
} socket_entry_t;

u_int sockets_alloc = 0;
Expand Down Expand Up @@ -1592,6 +1596,10 @@ process_ext_ecdh(socket_entry_t *e, struct sshbuf *buf)
}
agent_piv_close(B_FALSE);

bunyan_log(BNY_INFO, "performed ECDH operation",
"partner_pk", BNY_SSHKEY, partner,
NULL);

if ((r = sshbuf_put_u8(msg, SSH_AGENT_SUCCESS)) != 0 ||
(r = sshbuf_put_string(msg, secret, seclen)) != 0)
fatal("%s: buffer error: %s", __func__, ssh_err(r));
Expand Down Expand Up @@ -1624,6 +1632,7 @@ process_ext_rebox(socket_entry_t *e, struct sshbuf *buf)
size_t seclen, outlen;
boolean_t canskip = B_TRUE;
enum piv_slot_auth rauth;
char *slotstr;

if ((msg = sshbuf_new()) == NULL)
fatal("%s: sshbuf_new failed", __func__);
Expand Down Expand Up @@ -1711,6 +1720,15 @@ process_ext_rebox(socket_entry_t *e, struct sshbuf *buf)
goto out;
}

slotstr = piv_slotid_to_string(piv_slot_id(slot));
bunyan_log(BNY_INFO, "opened ECDH box",
"key_slot", BNY_STRING, slotstr,
"partner_pk", BNY_SSHKEY, partner,
"ephem_pk", BNY_SSHKEY, piv_box_ephem_pubkey(box),
"payload_size", BNY_SIZE_T, piv_box_encsize(box),
NULL);
free(slotstr);

VERIFY0(piv_box_take_data(box, &secret, &seclen));
agent_piv_close(B_FALSE);

Expand Down Expand Up @@ -2304,9 +2322,14 @@ process_message(u_int socknum)
"fd", BNY_INT, e->se_fd,
"msg_type", BNY_INT, (int)type,
"msg_type_name", BNY_STRING, msg_type_to_name(type),
"remote_uid", BNY_INT, (int)e->se_uid,
"remote_pid", BNY_INT, (int)e->se_pid,
"remote_cmd", BNY_STRING,
(e->se_exepath == NULL) ? "???" : e->se_exepath,
#if defined(__sun)
"remote_zid", BNY_INT, (int)e->se_zid,
"remote_zone", BNY_STRING, e->se_zname,
#endif
NULL);
bunyan_log(BNY_DEBUG, "received ssh-agent message", NULL);

Expand Down Expand Up @@ -2412,7 +2435,6 @@ check_socket_access(int fd, socket_entry_t *ent)
FILE *f;
ucred_t *peer = NULL;
struct psinfo *psinfo;
zoneid_t zid;
char fn[128];

if (getpeerucred(fd, &peer) != 0) {
Expand All @@ -2422,7 +2444,10 @@ check_socket_access(int fd, socket_entry_t *ent)
ent->se_uid = (euid = ucred_geteuid(peer));
ent->se_gid = ucred_getegid(peer);
ent->se_pid = ucred_getpid(peer);
zid = ucred_getzoneid(peer);
ent->se_zid = ucred_getzoneid(peer);
ent->se_zname[0] = '\0';
(void) getzonenamebyid(ent->se_zid, ent->se_zname,
sizeof (ent->se_zname));
ucred_free(peer);
psinfo = calloc(1, sizeof (struct psinfo));
snprintf(fn, sizeof (fn), "/proc/%d/psinfo", (int)ent->se_pid);
Expand All @@ -2437,9 +2462,9 @@ check_socket_access(int fd, socket_entry_t *ent)
fclose(f);
}
free(psinfo);
if (!allow_any_zoneid && !check_zid(zid)) {
if (!allow_any_zoneid && !check_zid(ent->se_zid)) {
error("zoneid mismatch: peer zoneid %u not on allow list",
(u_int) zid);
(u_int) ent->se_zid);
return (0);
}
if (!allow_any_uid && (euid != 0) && !check_uid(euid)) {
Expand Down

0 comments on commit 5d54eee

Please sign in to comment.