Skip to content

Commit

Permalink
cmd/snap-confine: add sc_init_invocation()
Browse files Browse the repository at this point in the history
The new helper function handles initialization of sc_invocation,
which mostly involves internal consistency checks.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
  • Loading branch information
zyga committed Mar 11, 2019
1 parent f6a589d commit b142edf
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ snap_confine_snap_confine_SOURCES = \
snap-confine/ns-support.h \
snap-confine/snap-confine-args.c \
snap-confine/snap-confine-args.h \
snap-confine/snap-confine-invocation.c \
snap-confine/snap-confine-invocation.h \
snap-confine/snap-confine-invocation.h \
snap-confine/snap-confine.c \
snap-confine/udev-support.c \
Expand Down
71 changes: 71 additions & 0 deletions cmd/snap-confine/snap-confine-invocation.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (C) 2019 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

#include "snap-confine-invocation.h"

#include <string.h>

#include "../libsnap-confine-private/snap.h"
#include "../libsnap-confine-private/utils.h"

void sc_init_invocation(sc_invocation * inv, const struct sc_args *args,
const char *snap_instance)
{
/* Snap instance name is conveyed via untrusted environment. It may be
* unset (typically when experimenting with snap-confine by hand). It
* must also be a valid snap instance name. */
if (snap_instance == NULL) {
die("SNAP_INSTANCE_NAME is not set");
}
sc_instance_name_validate(snap_instance, NULL);

/* The security tag is conveyed via untrusted command line. It must be
* in agreement with snap instance name and must be a valid security
* tag. */
const char *security_tag = sc_args_security_tag(args);
if (!verify_security_tag(security_tag, snap_instance)) {
die("security tag %s not allowed", security_tag);
}

/* The base snap name is conveyed via untrusted, optional, command line
* argument. It may be omitted where it implies the "core" snap is the
* base. */
const char *base_snap_name = sc_args_base_snap(args) ? : "core";
sc_snap_name_validate(base_snap_name, NULL);

/* The executable is conveyed via untrusted command lne. It must be set
* but cannot be validated further than that at this time. It might be
* arguable to validate it to be snap-exec in one of the well-known
* locations or one of the special-cases like strace / gdb but this is
* not done at this time. */
const char *executable = sc_args_executable(args);
/* TODO: validate NULL */

/* Invocation helps to pass relevant data to various parts of snap-confine. */
memset(inv, 0, sizeof *inv);
inv->base_snap_name = base_snap_name;
inv->executable = executable;
inv->security_tag = security_tag;
inv->snap_instance = snap_instance;
inv->classic_confinement = sc_args_is_classic_confinement(args);

debug("security tag: %s", inv->security_tag);
debug("executable: %s", inv->executable);
debug("confinement: %s",
inv->classic_confinement ? "classic" : "non-classic");
debug("base snap: %s", inv->base_snap_name);
}
12 changes: 12 additions & 0 deletions cmd/snap-confine/snap-confine-invocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#include <stdbool.h>

#include "snap-confine-args.h"

/**
* sc_invocation contains information about how snap-confine was invoked.
*
Expand All @@ -36,4 +38,14 @@ typedef struct sc_invocation {
bool is_normal_mode;
} sc_invocation;

/**
* sc_init_invocation initializes the invocation object.
*
* Invocation is constructed based on command line arguments as well as
* environment value (SNAP_INSTANCE_NAME). All input is untrustee and is
* validated internally.
**/
void sc_init_invocation(sc_invocation * inv, const struct sc_args *args,
const char *snap_instance);

#endif

0 comments on commit b142edf

Please sign in to comment.