cmd/snap-confine: various small fixes and tweaks to seccomp support code #3518

Merged
merged 15 commits into from Jul 6, 2017

Conversation

Projects
None yet
4 participants
Contributor

zyga commented Jun 23, 2017

This branch is a result of top-to-bottom read and tweak of seccomp support code.
There are several fixes for missing NULL checks, simplifications to memory management,
consistency tweaks for error messages, for error checks and for const correctness.

mvo5 approved these changes Jun 23, 2017

These are nice changes and thanks for spotting the lack of NULL checks (shame that wasn't found in the initial PR).

I didn't have an opportunity to review the wait PR; can you use strtol() instead of atoi()? While the use of atoi() looks ok here, strtol() is best practice.

- die("Out of memory creating checked_path");
+ size_t checked_path_size = strlen(path) + 1;
+ char *checked_path __attribute__ ((cleanup(sc_cleanup_string))) = NULL;
+ checked_path = calloc(checked_path_size, 1);
@jdstrand

jdstrand Jun 23, 2017

Contributor

I know that '1' is equivalent to sizeof(char), but I think that sizeof(char) declares intent of use better for the calloc and to a lesser extent checked_path_size.

cmd/snap-confine/seccomp-support.c
+ if (MAX_PROFILE_WAIT != NULL) {
+ int env_max_wait = atoi(MAX_PROFILE_WAIT);
+ max_wait = env_max_wait > 0 ? env_max_wait : max_wait;
+ }
@jdstrand

jdstrand Jun 23, 2017

Contributor

Can we also clean this up to use strtol() instead of atoi() (it has better error checking). Eg:

long max_wait = 120;
const char *MAX_PROFILE_WAIT = getenv("SNAP_CONFINE_MAX_PROFILE_WAIT");
if (MAX_PROFILE_WAIT != NULL) {
        char *endptr = NULL;
        errno = 0;
        long env_max_wait = strtol(MAX_PROFILE_WAIT, &endptr, 10);
        if (errno != 0 || MAX_PROFILE_WAIT == endptr || *endptr != '\0' || env_max_wait <= 0) {
                die("SNAP_CONFINE_MAX_PROFILE_WAIT invalid");
        }
	max_wait = env_max_wait > 0 ? env_max_wait : max_wait;
}
...
@zyga

zyga Jul 5, 2017

Contributor

Done, I'll push in a moment

cmd/snap-confine/seccomp-support.c
- perror
- ("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, ...) failed");
- die("aborting");
+ if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) < 0) {
@jdstrand

jdstrand Jun 23, 2017

Contributor

I prefer the specificity of != 0 rather than < 0 here and above, but these changes are not wrong.

@zyga

zyga Jul 5, 2017

Contributor

Done

Contributor

zyga commented Jul 5, 2017

I'll close and address feedback shortly.

@zyga zyga closed this Jul 5, 2017

zyga added some commits Jun 23, 2017

cmd/snap-confine: use getenv/atoi fewer times
Just a small code refactor to call getenv and atoi once instead of
twice.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/snap-confine: simplify polling code with access
Instead of stat'ing the binary seccomp profile file we can just use
access(2), it is slightly shorter and serves the same purpose.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/snap-confine: use PATH_MAX rather than arbitrary number
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/snap-confine: use correct constant declaration
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/snap-confine: use curly braces consistently
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/snap-confine: check for NULL values from strdup
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/snap-confine: use die rather than perror + die
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/snap-confine: use cleanup attribute for strings
The cleanup attribute automatically ensures that proper cleanup is done
in all cases and is less error prone to use.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/snap-confine: tweak error message for consistency
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/snap-confine: sizeof(char) is defined as 1
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/snap-confine: prefer calloc over malloc
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/snap-confine: test prctl return value to != 0
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/snap-confine: tweak error checking for consistency
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/snap-confine: use strtol instead of atoi
This allows us to detect errors correctly.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>

@zyga zyga reopened this Jul 5, 2017

codecov-io commented Jul 5, 2017

Codecov Report

Merging #3518 into master will decrease coverage by <.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3518      +/-   ##
==========================================
- Coverage   76.76%   76.75%   -0.01%     
==========================================
  Files         379      379              
  Lines       26277    26277              
==========================================
- Hits        20171    20169       -2     
- Misses       4314     4315       +1     
- Partials     1792     1793       +1
Impacted Files Coverage Δ
overlord/snapstate/snapstate.go 81.28% <0%> (-0.24%) ⬇️
overlord/ifacestate/helpers.go 65.54% <0%> (ø) ⬆️
interfaces/builtin/firewall_control.go 100% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bc47a43...373786e. Read the comment docs.

+1 conditional on max_int comment is addressed.

+ || env_max_wait <= 0) {
+ die("SNAP_CONFINE_MAX_PROFILE_WAIT invalid");
+ }
+ max_wait = env_max_wait > 0 ? env_max_wait : max_wait;
@jdstrand

jdstrand Jul 5, 2017

Contributor

You might overflow max_wait here. You should use long max_wait = 120 up above to avoid that (or cap the maximum wait to something 'reasonable' (eg, 3600) that fits in max_wait). This will only effectively skip the for loop, but should fix it regardless.

@zyga

zyga Jul 6, 2017

Contributor

Done, good catch.

cmd/snap-confine: make sure max_wait doesn't overflow
This patch uses long for all the variables involving max_wait and adds
an artificial cap of 3600 waiting time (in seconds).

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>

@zyga zyga merged commit 2d483fe into snapcore:master Jul 6, 2017

4 of 7 checks passed

artful-amd64 autopkgtest running
Details
xenial-amd64 autopkgtest running
Details
xenial-ppc64el autopkgtest running
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
xenial-i386 autopkgtest finished (success)
Details
yakkety-amd64 autopkgtest finished (success)
Details
zesty-amd64 autopkgtest finished (success)
Details

@zyga zyga deleted the zyga:tweak/waiting-for-profiles branch Jul 6, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment