snap-confine: validate SNAP_NAME against security tag #3476

Merged
merged 6 commits into from Jun 19, 2017

Conversation

Projects
None yet
5 participants
Contributor

stolowski commented Jun 13, 2017

Make sure SNAP_NAME env variable matches the name from security tag.

Quick comments

cmd/libsnap-confine-private/snap.c
- int status = regexec(&re, security_tag, 0, NULL, 0);
+ // first capture is the entire string, second is the name we care about
+ regmatch_t matches[2];
+ int status = regexec(&re, security_tag, sizeof(matches)/sizeof(regmatch_t), matches, 0);
@zyga

zyga Jun 13, 2017

Contributor

sizeof matches / sizeof *matches

cmd/libsnap-confine-private/snap.c
regfree(&re);
- return (status == 0);
+ if (status != 0 || matches[1].rm_so < 0) {
@zyga

zyga Jun 13, 2017

Contributor

Can you please document that this checks the sub-match associated for snap name was actually found.

- g_assert_false(verify_security_tag("snap..name.app"));
- g_assert_false(verify_security_tag("snap.name..app"));
- g_assert_false(verify_security_tag("snap.name.app.."));
+ g_assert_false(verify_security_tag
@zyga

zyga Jun 13, 2017

Contributor

Can you move this test to the end of the function and add a comment to explaining that it snap name and security tag are not matching (though are separately valid).

@stolowski

stolowski Jun 13, 2017

Contributor

Ok, done.

cmd/libsnap-confine-private/snap.c
@@ -27,25 +27,34 @@
#include "string-utils.h"
#include "cleanup-funcs.h"
-bool verify_security_tag(const char *security_tag)
+bool verify_security_tag(const char *security_tag, const char *snap_name)
{
// The executable name is of form:
@zyga

zyga Jun 13, 2017

Contributor

Can you move this to the documentation of the function in the header file please

zyga approved these changes Jun 13, 2017

+1, thanks

@zyga zyga requested a review from jdstrand Jun 13, 2017

codecov-io commented Jun 14, 2017

Codecov Report

Merging #3476 into master will decrease coverage by 0.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3476      +/-   ##
==========================================
- Coverage   77.16%   77.14%   -0.02%     
==========================================
  Files         373      373              
  Lines       25793    25793              
==========================================
- Hits        19902    19899       -3     
- Misses       4134     4136       +2     
- Partials     1757     1758       +1
Impacted Files Coverage Δ
interfaces/sorting.go 86.66% <0%> (-6.67%) ⬇️
overlord/snapstate/snapstate.go 81.28% <0%> (-0.24%) ⬇️
overlord/ifacestate/helpers.go 65.54% <0%> (ø) ⬆️
cmd/snap/cmd_aliases.go 96% <0%> (+2%) ⬆️

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 1495246...11dd1a9. Read the comment docs.

Thanks for this. Approving (though please make the requested comment changes before merging).

cmd/libsnap-confine-private/snap.c
die("can not compile regex %s", whitelist_re);
- int status = regexec(&re, security_tag, 0, NULL, 0);
+ // first capture is the entire string, second is the name we care about
@jdstrand

jdstrand Jun 14, 2017

Contributor

This comment isn't quite right. We actually care about both because we want to make sure the whole string matches the regex and that the snap name matches a specific substring. I suggest updating this comment to be:

// first capture is for verifying the full security tag, second capture
// for verifying the snap_name is correct for this security tag
cmd/libsnap-confine-private/snap.c
regfree(&re);
- return (status == 0);
+ // make sure that snap name was captured by 2nd match group
@jdstrand

jdstrand Jun 14, 2017

Contributor

Please adjust the comment to be:

// Fail if no match or if snap name wasn't captured in the 2nd match group
+ * - <hookname must start with a lowercase letter, then may
+ * contain lowercase letters and '-'
+ **/
+bool verify_security_tag(const char *security_tag, const char *snap_name);
@jdstrand

jdstrand Jun 14, 2017

Contributor

This change is fine. While it breaks API, this is in a private library and all callers of verify_security_tag() have either been removed or updated for the new arg.

- if (snap_name == NULL) {
- die("SNAP_NAME is not set");
- }
- sc_snap_name_validate(snap_name, NULL);
sc_snap_name_validate(base_snap_name, NULL);
debug("security tag: %s", security_tag);
@jdstrand

jdstrand Jun 14, 2017

Contributor

The changes to snap-confine.c look fine-- it only moves the check and setting of snap_name up above verify_security_tag() and passes snap_name to verify_security_tag().

@@ -157,9 +153,6 @@ void setup_devices_cgroup(const char *security_tag, struct snappy_udev *udev_s)
NULL,
};
- // extra paranoia
- if (!verify_security_tag(security_tag))
- die("security tag %s not allowed", security_tag);
if (udev_s == NULL)
die("snappy_udev is NULL");
if (udev_s->udev == NULL)
@jdstrand

jdstrand Jun 14, 2017

Contributor

The changes to udev-support.c look fine-- snappy_udev_init() and setup_devices_cgroup() are only used in snap-confine.c both called after verify_security_tag()

stolowski added some commits Jun 19, 2017

mvo5 approved these changes Jun 19, 2017

@mvo5 mvo5 merged commit cb3b286 into snapcore:master Jun 19, 2017

4 of 7 checks passed

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

stolowski commented Jun 19, 2017

@jdstrand Thanks for the review, I've fixed the comments per your suggestions.

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