1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2021-2024 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/>.
*
*/
package daemon_test
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/client"
"github.com/snapcore/snapd/daemon"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/overlord/auth"
"github.com/snapcore/snapd/polkit"
"github.com/snapcore/snapd/testutil"
)
type accessSuite struct {
apiBaseSuite
}
var _ = Suite(&accessSuite{})
var (
errForbidden = daemon.Forbidden("access denied")
errUnauthorized = daemon.Unauthorized("access denied")
)
func (s *accessSuite) TestAccessOptionsValidation(c *C) {
opts := daemon.AccessOptions{}
c.Check(daemon.CheckAccess(nil, nil, nil, nil, opts), ErrorMatches, `unexpected access level "" \(api 500\)`)
opts = daemon.AccessOptions{AccessLevel: "some-level"}
c.Check(daemon.CheckAccess(nil, nil, nil, nil, opts), ErrorMatches, `unexpected access level "some-level" \(api 500\)`)
opts = daemon.AccessOptions{AccessLevel: "root"}
c.Check(daemon.CheckAccess(nil, nil, nil, nil, opts), ErrorMatches, `no sockets specified \(api 500\)`)
opts = daemon.AccessOptions{AccessLevel: "root", Sockets: []string{"some-socket"}}
c.Check(daemon.CheckAccess(nil, nil, nil, nil, opts), ErrorMatches, `unexpected socket "some-socket" \(api 500\)`)
}
func (s *accessSuite) TestOpenAccess(c *C) {
var ac daemon.AccessChecker = daemon.OpenAccess{}
// openAccess denies access from snapd-snap.socket
ucred := &daemon.Ucrednet{Uid: 42, Pid: 100, Socket: dirs.SnapSocket}
c.Check(ac.CheckAccess(nil, nil, ucred, nil), DeepEquals, errForbidden)
// Access allowed from snapd.socket
ucred.Socket = dirs.SnapdSocket
c.Check(ac.CheckAccess(nil, nil, ucred, nil), IsNil)
// Access forbidden without peer credentials. This will need
// to be revisited if the API is ever exposed over TCP.
c.Check(ac.CheckAccess(nil, nil, nil, nil), DeepEquals, errForbidden)
}
func (s *accessSuite) TestAuthenticatedAccess(c *C) {
restore := daemon.MockCheckPolkitAction(func(r *http.Request, ucred *daemon.Ucrednet, action string) *daemon.APIError {
// Polkit is not consulted if no action is specified
c.Fail()
return daemon.Forbidden("access denied")
})
defer restore()
var ac daemon.AccessChecker = daemon.AuthenticatedAccess{}
req := httptest.NewRequest("GET", "/", nil)
user := &auth.UserState{}
// authenticatedAccess denies access from snapd-snap.socket
ucred := &daemon.Ucrednet{Uid: 0, Pid: 100, Socket: dirs.SnapSocket}
c.Check(ac.CheckAccess(nil, req, ucred, nil), DeepEquals, errForbidden)
c.Check(ac.CheckAccess(nil, req, ucred, user), DeepEquals, errForbidden)
// the same for unknown sockets
ucred = &daemon.Ucrednet{Uid: 0, Pid: 100, Socket: "unexpected.socket"}
c.Check(ac.CheckAccess(nil, req, ucred, nil), DeepEquals, errForbidden)
// With macaroon auth, a normal user is granted access
ucred = &daemon.Ucrednet{Uid: 42, Pid: 100, Socket: dirs.SnapdSocket}
c.Check(ac.CheckAccess(nil, req, ucred, user), IsNil)
// Macaroon access requires peer credentials
c.Check(ac.CheckAccess(nil, req, nil, user), DeepEquals, errForbidden)
// Without macaroon auth, normal users are unauthorized
c.Check(ac.CheckAccess(nil, req, ucred, nil), DeepEquals, errUnauthorized)
// The root user is granted access without a macaroon
ucred = &daemon.Ucrednet{Uid: 0, Pid: 100, Socket: dirs.SnapdSocket}
c.Check(ac.CheckAccess(nil, req, ucred, nil), IsNil)
}
func (s *accessSuite) TestAuthenticatedAccessPolkit(c *C) {
var ac daemon.AccessChecker = daemon.AuthenticatedAccess{Polkit: "action-id"}
req := httptest.NewRequest("GET", "/", nil)
user := &auth.UserState{}
ucred := &daemon.Ucrednet{Uid: 0, Pid: 100, Socket: dirs.SnapdSocket}
// polkit is not checked if any of:
// * ucred is missing
// * macaroon auth is provided
// * user is root
restore := daemon.MockCheckPolkitAction(func(r *http.Request, ucred *daemon.Ucrednet, action string) *daemon.APIError {
c.Fail()
return daemon.Forbidden("access denied")
})
defer restore()
c.Check(ac.CheckAccess(nil, req, nil, nil), DeepEquals, errForbidden)
c.Check(ac.CheckAccess(nil, req, nil, user), DeepEquals, errForbidden)
c.Check(ac.CheckAccess(nil, req, ucred, nil), IsNil)
// polkit is checked for regular users without macaroon auth
restore = daemon.MockCheckPolkitAction(func(r *http.Request, u *daemon.Ucrednet, action string) *daemon.APIError {
c.Check(r, Equals, req)
c.Check(u, Equals, ucred)
c.Check(action, Equals, "action-id")
return nil
})
defer restore()
ucred = &daemon.Ucrednet{Uid: 42, Pid: 100, Socket: dirs.SnapdSocket}
c.Check(ac.CheckAccess(nil, req, ucred, nil), IsNil)
}
func (s *accessSuite) TestCheckPolkitActionImpl(c *C) {
logbuf, restore := logger.MockLogger()
defer restore()
req := httptest.NewRequest("GET", "/", nil)
ucred := &daemon.Ucrednet{Uid: 42, Pid: 1000, Socket: dirs.SnapdSocket}
// Access granted if polkit authorizes the request
restore = daemon.MockPolkitCheckAuthorization(func(pid int32, uid uint32, actionId string, details map[string]string, flags polkit.CheckFlags) (bool, error) {
c.Check(pid, Equals, int32(1000))
c.Check(uid, Equals, uint32(42))
c.Check(actionId, Equals, "action-id")
c.Check(details, IsNil)
c.Check(flags, Equals, polkit.CheckFlags(0))
return true, nil
})
defer restore()
c.Check(daemon.CheckPolkitActionImpl(req, ucred, "action-id"), IsNil)
c.Check(logbuf.String(), Equals, "")
// Unauthorized if polkit denies the request
restore = daemon.MockPolkitCheckAuthorization(func(pid int32, uid uint32, actionId string, details map[string]string, flags polkit.CheckFlags) (bool, error) {
return false, nil
})
defer restore()
c.Check(daemon.CheckPolkitActionImpl(req, ucred, "action-id"), DeepEquals, errUnauthorized)
c.Check(logbuf.String(), Equals, "")
// Cancelled if the user dismisses the auth check
restore = daemon.MockPolkitCheckAuthorization(func(pid int32, uid uint32, actionId string, details map[string]string, flags polkit.CheckFlags) (bool, error) {
return false, polkit.ErrDismissed
})
defer restore()
rspe := daemon.CheckPolkitActionImpl(req, ucred, "action-id")
c.Check(rspe, DeepEquals, daemon.AuthCancelled("cancelled"))
c.Check(logbuf.String(), Equals, "")
// The X-Allow-Interaction header can be set to tell polkitd
// that interaction with the user is allowed.
req.Header.Set(client.AllowInteractionHeader, "true")
restore = daemon.MockPolkitCheckAuthorization(func(pid int32, uid uint32, actionId string, details map[string]string, flags polkit.CheckFlags) (bool, error) {
c.Check(flags, Equals, polkit.CheckFlags(polkit.CheckAllowInteraction))
return true, nil
})
defer restore()
c.Check(daemon.CheckPolkitActionImpl(req, ucred, "action-id"), IsNil)
c.Check(logbuf.String(), Equals, "")
// Bad values in the request header are logged
req.Header.Set(client.AllowInteractionHeader, "garbage")
restore = daemon.MockPolkitCheckAuthorization(func(pid int32, uid uint32, actionId string, details map[string]string, flags polkit.CheckFlags) (bool, error) {
c.Check(flags, Equals, polkit.CheckFlags(0))
return true, nil
})
defer restore()
c.Check(daemon.CheckPolkitActionImpl(req, ucred, "action-id"), IsNil)
c.Check(logbuf.String(), testutil.Contains, "error parsing X-Allow-Interaction header:")
}
func (s *accessSuite) TestRootAccess(c *C) {
var ac daemon.AccessChecker = daemon.RootAccess{}
user := &auth.UserState{}
// rootAccess denies access without ucred
c.Check(ac.CheckAccess(nil, nil, nil, nil), DeepEquals, errForbidden)
c.Check(ac.CheckAccess(nil, nil, nil, user), DeepEquals, errForbidden)
// rootAccess denies access from snapd-snap.socket
ucred := &daemon.Ucrednet{Uid: 0, Pid: 100, Socket: dirs.SnapSocket}
c.Check(ac.CheckAccess(nil, nil, ucred, nil), DeepEquals, errForbidden)
c.Check(ac.CheckAccess(nil, nil, ucred, user), DeepEquals, errForbidden)
// Non-root users are forbidden, even with macaroon auth
ucred = &daemon.Ucrednet{Uid: 42, Pid: 100, Socket: dirs.SnapdSocket}
c.Check(ac.CheckAccess(nil, nil, ucred, nil), DeepEquals, errForbidden)
c.Check(ac.CheckAccess(nil, nil, ucred, user), DeepEquals, errForbidden)
// Root is granted access
ucred = &daemon.Ucrednet{Uid: 0, Pid: 100, Socket: dirs.SnapdSocket}
c.Check(ac.CheckAccess(nil, nil, ucred, nil), IsNil)
}
func (s *accessSuite) TestSnapAccess(c *C) {
var ac daemon.AccessChecker = daemon.SnapAccess{}
// snapAccess allows access from snapd-snap.socket
ucred := &daemon.Ucrednet{Uid: 42, Pid: 100, Socket: dirs.SnapSocket}
c.Check(ac.CheckAccess(nil, nil, ucred, nil), IsNil)
// access is forbidden on the main socket or without peer creds
ucred.Socket = dirs.SnapdSocket
c.Check(ac.CheckAccess(nil, nil, ucred, nil), DeepEquals, errForbidden)
c.Check(ac.CheckAccess(nil, nil, nil, nil), DeepEquals, errForbidden)
}
func (s *accessSuite) TestRequireInterfaceApiAccessImpl(c *C) {
d := s.daemon(c)
s.mockSnap(c, `
name: core
type: os
version: 1
slots:
snap-themes-control:
snap-refresh-control:
`)
s.mockSnap(c, `
name: some-snap
version: 1
plugs:
snap-themes-control:
snap-refresh-control:
`)
restore := daemon.MockCgroupSnapNameFromPid(func(pid int) (string, error) {
if pid == 42 {
return "some-snap", nil
}
return "", fmt.Errorf("not a snap")
})
defer restore()
var ac daemon.AccessChecker = daemon.InterfaceOpenAccess{Interfaces: []string{"snap-themes-control", "snap-refresh-control"}}
// Access with no ucred data is forbidden
c.Check(ac.CheckAccess(d, nil, nil, nil), DeepEquals, errForbidden)
// Access from snapd.socket is allowed
ucred := &daemon.Ucrednet{Uid: 1000, Pid: 1001, Socket: dirs.SnapdSocket}
req := http.Request{RemoteAddr: ucred.String()}
c.Check(ac.CheckAccess(d, nil, ucred, nil), IsNil)
c.Check(req.RemoteAddr, Equals, ucred.String())
// Access from unknown sockets is forbidden
ucred = &daemon.Ucrednet{Uid: 1000, Pid: 1001, Socket: "unknown.socket"}
c.Check(ac.CheckAccess(d, nil, ucred, nil), DeepEquals, errForbidden)
// Access from pids that cannot be mapped to a snap on
// snapd-snap.socket are rejected
ucred = &daemon.Ucrednet{Uid: 1000, Pid: 1001, Socket: dirs.SnapSocket}
c.Check(ac.CheckAccess(d, nil, ucred, nil), DeepEquals, daemon.Forbidden("could not determine snap name for pid: not a snap"))
// Access from snapd-snap.socket is rejected by default
ucred = &daemon.Ucrednet{Uid: 1000, Pid: 42, Socket: dirs.SnapSocket}
c.Check(ac.CheckAccess(d, nil, ucred, nil), DeepEquals, errForbidden)
// Now connect the marker interface
st := d.Overlord().State()
st.Lock()
st.Set("conns", map[string]any{
"some-snap:snap-themes-control core:snap-themes-control": map[string]any{
"interface": "snap-themes-control",
},
})
st.Unlock()
// Access is allowed now that the snap has the plug connected
req = http.Request{RemoteAddr: ucred.String()}
c.Check(ac.CheckAccess(s.d, &req, ucred, nil), IsNil)
// Interface is attached to RemoteAddr
c.Check(req.RemoteAddr, Equals, fmt.Sprintf("%siface=snap-themes-control;", ucred))
// Now connect both interfaces
st.Lock()
st.Set("conns", map[string]any{
"some-snap:snap-themes-control core:snap-themes-control": map[string]any{
"interface": "snap-themes-control",
},
"some-snap:snap-refresh-control core:snap-refresh-control": map[string]any{
"interface": "snap-refresh-control",
},
})
st.Unlock()
req = http.Request{RemoteAddr: ucred.String()}
c.Check(ac.CheckAccess(s.d, &req, ucred, nil), IsNil)
// Check that both interfaces are attached to RemoteAddr.
// Since conns is a map, order is not guaranteed.
c.Check(req.RemoteAddr, Matches, fmt.Sprintf("^%siface=(snap-themes-control&snap-refresh-control|snap-refresh-control&snap-themes-control);$", ucred))
// A left over "undesired" connection does not grant access
st.Lock()
st.Set("conns", map[string]any{
"some-snap:snap-themes-control core:snap-themes-control": map[string]any{
"interface": "snap-themes-control",
"undesired": true,
},
})
st.Unlock()
req = http.Request{RemoteAddr: ucred.String()}
c.Check(ac.CheckAccess(d, nil, ucred, nil), DeepEquals, errForbidden)
c.Check(req.RemoteAddr, Equals, ucred.String())
}
func (s *accessSuite) TestInterfaceOpenAccess(c *C) {
var ac daemon.AccessChecker = daemon.InterfaceOpenAccess{Interfaces: []string{"snap-themes-control", "snap-interfaces-requests-control"}}
s.daemon(c)
// interfaceOpenAccess allows access if requireInterfaceApiAccess() succeeds
ucred := &daemon.Ucrednet{Uid: 42, Pid: 100, Socket: dirs.SnapSocket}
restore := daemon.MockRequireInterfaceApiAccess(func(
d *daemon.Daemon, r *http.Request, u *daemon.Ucrednet, reqs daemon.InterfaceAccessReqs,
) *daemon.APIError {
c.Check(d, Equals, s.d)
c.Check(u, Equals, ucred)
c.Check(reqs, DeepEquals, daemon.InterfaceAccessReqs{
Interfaces: []string{"snap-themes-control", "snap-interfaces-requests-control"},
Plug: true,
})
return nil
})
defer restore()
c.Check(ac.CheckAccess(s.d, nil, ucred, nil), IsNil)
// Access is forbidden if requireInterfaceApiAccess() fails
restore = daemon.MockRequireInterfaceApiAccess(func(
d *daemon.Daemon, r *http.Request, u *daemon.Ucrednet, req daemon.InterfaceAccessReqs,
) *daemon.APIError {
return errForbidden
})
defer restore()
c.Check(ac.CheckAccess(s.d, nil, ucred, nil), DeepEquals, errForbidden)
}
func (s *accessSuite) TestInterfaceAuthenticatedAccess(c *C) {
restore := daemon.MockCheckPolkitAction(func(r *http.Request, ucred *daemon.Ucrednet, action string) *daemon.APIError {
// Polkit is not consulted if no action is specified
c.Fail()
return errForbidden
})
defer restore()
var ac daemon.AccessChecker = daemon.InterfaceAuthenticatedAccess{}
req := httptest.NewRequest("GET", "/", nil)
user := &auth.UserState{}
s.daemon(c)
// interfaceAuthenticatedAccess denies access if requireInterfaceApiAccess fails
ucred := &daemon.Ucrednet{Uid: 0, Pid: 100, Socket: dirs.SnapSocket}
restore = daemon.MockRequireInterfaceApiAccess(func(
d *daemon.Daemon, r *http.Request, u *daemon.Ucrednet, reqs daemon.InterfaceAccessReqs,
) *daemon.APIError {
c.Check(d, Equals, s.d)
c.Check(u, Equals, ucred)
c.Check(reqs, DeepEquals, daemon.InterfaceAccessReqs{
Plug: true,
})
return errForbidden
})
defer restore()
c.Check(ac.CheckAccess(s.d, req, ucred, nil), DeepEquals, errForbidden)
c.Check(ac.CheckAccess(s.d, req, ucred, user), DeepEquals, errForbidden)
// If requireInterfaceApiAccess succeeds, root is granted access
restore = daemon.MockRequireInterfaceApiAccess(func(
d *daemon.Daemon, r *http.Request, u *daemon.Ucrednet, reqs daemon.InterfaceAccessReqs,
) *daemon.APIError {
return nil
})
defer restore()
c.Check(ac.CheckAccess(s.d, req, ucred, nil), IsNil)
// Macaroon auth will grant a normal user access too
ucred = &daemon.Ucrednet{Uid: 42, Pid: 100, Socket: dirs.SnapSocket}
c.Check(ac.CheckAccess(s.d, req, ucred, user), IsNil)
// Without macaroon auth, normal users are unauthorized
c.Check(ac.CheckAccess(s.d, req, ucred, nil), DeepEquals, errUnauthorized)
}
func (s *accessSuite) TestInterfaceAuthenticatedAccessPolkit(c *C) {
var ac daemon.AccessChecker = daemon.InterfaceAuthenticatedAccess{Polkit: "action-id"}
req := httptest.NewRequest("GET", "/", nil)
user := &auth.UserState{}
ucred := &daemon.Ucrednet{Uid: 0, Pid: 100, Socket: dirs.SnapSocket}
s.daemon(c)
restore := daemon.MockRequireInterfaceApiAccess(func(
d *daemon.Daemon, r *http.Request, u *daemon.Ucrednet, reqs daemon.InterfaceAccessReqs,
) *daemon.APIError {
c.Check(d, Equals, s.d)
c.Check(u, Equals, ucred)
c.Check(reqs, DeepEquals, daemon.InterfaceAccessReqs{
Plug: true,
})
return nil
})
defer restore()
// polkit is not checked if any of:
// * user is root
// * regular users with macaroon auth
restore = daemon.MockCheckPolkitAction(func(r *http.Request, ucred *daemon.Ucrednet, action string) *daemon.APIError {
c.Fail()
return errForbidden
})
defer restore()
c.Check(ac.CheckAccess(s.d, req, ucred, nil), IsNil)
ucred = &daemon.Ucrednet{Uid: 42, Pid: 100, Socket: dirs.SnapdSocket}
c.Check(ac.CheckAccess(s.d, req, ucred, user), IsNil)
// polkit is checked for regular users without macaroon auth
restore = daemon.MockCheckPolkitAction(func(r *http.Request, u *daemon.Ucrednet, action string) *daemon.APIError {
c.Check(r, Equals, req)
c.Check(u, Equals, ucred)
c.Check(action, Equals, "action-id")
return nil
})
defer restore()
c.Check(ac.CheckAccess(s.d, req, ucred, nil), IsNil)
}
func (s *accessSuite) TestInterfaceProviderRootAccessCallsWithCorrectArgs(c *C) {
restore := daemon.MockCheckPolkitAction(func(r *http.Request, ucred *daemon.Ucrednet, action string) *daemon.APIError {
// Polkit is not consulted if no action is specified
c.Fail()
return errForbidden
})
defer restore()
var ac daemon.AccessChecker = daemon.InterfaceProviderRootAccess{
Interfaces: []string{"fwupd"},
}
req := httptest.NewRequest("GET", "/", nil)
s.daemon(c)
ucred := &daemon.Ucrednet{Uid: 0, Pid: 100, Socket: dirs.SnapSocket}
// mock and check whether correct arguments are passed
called := 0
restore = daemon.MockRequireInterfaceApiAccess(func(
d *daemon.Daemon, r *http.Request, u *daemon.Ucrednet, reqs daemon.InterfaceAccessReqs,
) *daemon.APIError {
c.Check(d, Equals, s.d)
c.Check(u, Equals, ucred)
c.Check(reqs, DeepEquals, daemon.InterfaceAccessReqs{
Interfaces: []string{"fwupd"},
Slot: true,
})
called++
return errForbidden
})
defer restore()
c.Check(ac.CheckAccess(s.d, req, ucred, nil), DeepEquals, errForbidden)
c.Assert(called, Equals, 1)
}
func (s *accessSuite) TestInterfaceProviderRootAccessChecks(c *C) {
d := s.daemon(c)
s.mockSnap(c, `
name: fwupd-app
type: app
version: 1
slots:
fwupd-provider:
interface: fwupd
plugs:
fwupd-consumer:
interface: fwupd
`)
s.mockSnap(c, `
name: connected-fwupd-caller
version: 1
plugs:
fwupd-consumer:
interface: fwupd
`)
s.mockSnap(c, `
name: disconnected-fwupd-caller
version: 1
plugs:
fwupd-consumer:
interface: fwupd
`)
restore := daemon.MockCgroupSnapNameFromPid(func(pid int) (string, error) {
switch pid {
case 42:
return "fwupd-app", nil
case 1042:
return "connected-fwupd-caller", nil
case 10042:
return "disconnected-fwupd-caller", nil
default:
return "", fmt.Errorf("not a snap")
}
})
defer restore()
restore = daemon.MockCheckPolkitAction(func(r *http.Request, ucred *daemon.Ucrednet, action string) *daemon.APIError {
// Polkit is not consulted if no action is specified
c.Fail()
return errForbidden
})
defer restore()
var ac daemon.AccessChecker = daemon.InterfaceProviderRootAccess{
Interfaces: []string{"fwupd"},
}
user := &auth.UserState{}
// fwupd-app, but unconnected and over snap socket
ucred := &daemon.Ucrednet{Uid: 0, Pid: 42, Socket: dirs.SnapSocket}
req := &http.Request{
RemoteAddr: ucred.String(),
}
c.Check(ac.CheckAccess(s.d, req, ucred, nil), DeepEquals, errForbidden)
// Now connect both interfaces
st := d.Overlord().State()
st.Lock()
st.Set("conns", map[string]any{
"fwupd-app:fwupd-consumer fwupd-app:fwupd-provider": map[string]any{
"interface": "fwupd",
},
"connected-fwupd-caller:fwupd fwupd-app:fwupd-provider": map[string]any{
"interface": "fwupd",
},
})
st.Unlock()
// fwupd-app, connected on the slot side
ucred = &daemon.Ucrednet{Uid: 0, Pid: 42, Socket: dirs.SnapSocket}
req = &http.Request{
RemoteAddr: ucred.String(),
}
c.Check(ac.CheckAccess(s.d, req, ucred, user), IsNil)
// connected-fwupd-caller, but on the plug side
ucred = &daemon.Ucrednet{Uid: 0, Pid: 1042, Socket: dirs.SnapSocket}
req = &http.Request{
RemoteAddr: ucred.String(),
}
c.Check(ac.CheckAccess(s.d, req, ucred, user), DeepEquals, errForbidden)
// disconnected-fwupd-caller
ucred = &daemon.Ucrednet{Uid: 0, Pid: 10042, Socket: dirs.SnapSocket}
req = &http.Request{
RemoteAddr: ucred.String(),
}
c.Check(ac.CheckAccess(s.d, req, ucred, user), DeepEquals, errForbidden)
// normal user has no access even with a Macaroon auth
ucred = &daemon.Ucrednet{Uid: 42, Pid: 42, Socket: dirs.SnapSocket}
req = &http.Request{
RemoteAddr: ucred.String(),
}
c.Check(ac.CheckAccess(s.d, req, ucred, user), DeepEquals, errUnauthorized)
// Without macaroon auth, normal users are unauthorized
c.Check(ac.CheckAccess(s.d, req, ucred, nil), DeepEquals, errUnauthorized)
// on snapd socket, non-root is unauthorized
ucred = &daemon.Ucrednet{Uid: 42, Pid: 123, Socket: dirs.SnapdSocket}
req = &http.Request{
RemoteAddr: ucred.String(),
}
c.Check(ac.CheckAccess(s.d, req, ucred, nil), DeepEquals, errUnauthorized)
// but root is
ucred = &daemon.Ucrednet{Uid: 0, Pid: 123, Socket: dirs.SnapdSocket}
req = &http.Request{
RemoteAddr: ucred.String(),
}
c.Check(ac.CheckAccess(s.d, req, ucred, nil), IsNil)
}
func (s *accessSuite) TestInterfaceRootAccessCallsWithCorrectArgs(c *C) {
restore := daemon.MockCheckPolkitAction(func(r *http.Request, ucred *daemon.Ucrednet, action string) *daemon.APIError {
// Polkit is not consulted if no action is specified
c.Fail()
return errForbidden
})
defer restore()
var ac daemon.AccessChecker = daemon.InterfaceRootAccess{
Interfaces: []string{"fwupd"},
}
req := httptest.NewRequest("GET", "/", nil)
s.daemon(c)
ucred := &daemon.Ucrednet{Uid: 0, Pid: 100, Socket: dirs.SnapSocket}
// mock and check whether correct arguments are passed
called := 0
restore = daemon.MockRequireInterfaceApiAccess(func(
d *daemon.Daemon, r *http.Request, u *daemon.Ucrednet, reqs daemon.InterfaceAccessReqs,
) *daemon.APIError {
c.Check(d, Equals, s.d)
c.Check(u, Equals, ucred)
c.Check(reqs, DeepEquals, daemon.InterfaceAccessReqs{
Interfaces: []string{"fwupd"},
Plug: true,
})
called++
return errForbidden
})
defer restore()
c.Check(ac.CheckAccess(s.d, req, ucred, nil), DeepEquals, errForbidden)
c.Assert(called, Equals, 1)
}
func (s *accessSuite) TestInterfaceRootAccessChecks(c *C) {
d := s.daemon(c)
s.mockSnap(c, `
name: fwupd-app
type: app
version: 1
slots:
fwupd-provider:
interface: fwupd
`)
s.mockSnap(c, `
name: connected-fwupd-caller
version: 1
plugs:
fwupd-consumer:
interface: fwupd
`)
restore := daemon.MockCgroupSnapNameFromPid(func(pid int) (string, error) {
switch pid {
case 42:
return "fwupd-app", nil
case 1042:
return "connected-fwupd-caller", nil
default:
return "", fmt.Errorf("not a snap")
}
})
defer restore()
restore = daemon.MockCheckPolkitAction(func(r *http.Request, ucred *daemon.Ucrednet, action string) *daemon.APIError {
// Polkit is not consulted if no action is specified
c.Fail()
return errForbidden
})
defer restore()
var ac daemon.AccessChecker = daemon.InterfaceRootAccess{
Interfaces: []string{"fwupd"},
}
user := &auth.UserState{}
// connected-fwupd-caller, but unconnected and over snap socket
ucred := &daemon.Ucrednet{Uid: 0, Pid: 1042, Socket: dirs.SnapSocket}
req := &http.Request{
RemoteAddr: ucred.String(),
}
c.Check(ac.CheckAccess(s.d, req, ucred, nil), DeepEquals, errForbidden)
// Now connect connected-fwupd-caller (plug) to fwupd-app (slot)
st := d.Overlord().State()
st.Lock()
st.Set("conns", map[string]any{
"connected-fwupd-caller:fwupd fwupd-app:fwupd-provider": map[string]any{
"interface": "fwupd",
},
})
st.Unlock()
// connected-fwupd-caller, connected on the plug side
ucred = &daemon.Ucrednet{Uid: 0, Pid: 1042, Socket: dirs.SnapSocket}
req = &http.Request{
RemoteAddr: ucred.String(),
}
c.Check(ac.CheckAccess(s.d, req, ucred, user), IsNil)
// fwupd-app, connected on the slot side
ucred = &daemon.Ucrednet{Uid: 0, Pid: 42, Socket: dirs.SnapSocket}
req = &http.Request{
RemoteAddr: ucred.String(),
}
c.Check(ac.CheckAccess(s.d, req, ucred, user), DeepEquals, errForbidden)
// normal user has no access even with a Macaroon auth
ucred = &daemon.Ucrednet{Uid: 42, Pid: 1042, Socket: dirs.SnapSocket}
req = &http.Request{
RemoteAddr: ucred.String(),
}
c.Check(ac.CheckAccess(s.d, req, ucred, user), DeepEquals, errUnauthorized)
// Without macaroon auth, normal users are unauthorized
c.Check(ac.CheckAccess(s.d, req, ucred, nil), DeepEquals, errUnauthorized)
// on snapd socket, non-root is unauthorized
ucred = &daemon.Ucrednet{Uid: 42, Pid: 123, Socket: dirs.SnapdSocket}
req = &http.Request{
RemoteAddr: ucred.String(),
}
c.Check(ac.CheckAccess(s.d, req, ucred, nil), DeepEquals, errUnauthorized)
// but root is
ucred = &daemon.Ucrednet{Uid: 0, Pid: 123, Socket: dirs.SnapdSocket}
req = &http.Request{
RemoteAddr: ucred.String(),
}
c.Check(ac.CheckAccess(s.d, req, ucred, nil), IsNil)
}
func (s *accessSuite) TestInterfaceRootAccessPolkit(c *C) {
d := s.daemon(c)
s.mockSnap(c, `
name: fwupd-app
type: app
version: 1
slots:
fwupd-provider:
interface: fwupd
`)
s.mockSnap(c, `
name: connected-fwupd-caller
version: 1
plugs:
fwupd-consumer:
interface: fwupd
`)
restore := daemon.MockCgroupSnapNameFromPid(func(pid int) (string, error) {
switch pid {
case 42:
return "fwupd-app", nil
case 1042:
return "connected-fwupd-caller", nil
default:
return "", fmt.Errorf("not a snap")
}
})
defer restore()
st := d.Overlord().State()
st.Lock()
st.Set("conns", map[string]any{
"connected-fwupd-caller:fwupd fwupd-app:fwupd-provider": map[string]any{
"interface": "fwupd",
},
})
st.Unlock()
var ac daemon.AccessChecker = daemon.InterfaceRootAccess{
Polkit: "action-id",
Interfaces: []string{"fwupd"},
}
req := httptest.NewRequest("GET", "/", nil)
user := &auth.UserState{}
// polkit is not checked if any of:
// * ucred is missing
// * user is root
// * snap request (as root) with relevant connected plug
// * snap request without relevant connected plug
restore = daemon.MockCheckPolkitAction(func(r *http.Request, ucred *daemon.Ucrednet, action string) *daemon.APIError {
c.Fail()
return daemon.Forbidden("access denied")
})
defer restore()
// ucred is missing
c.Check(ac.CheckAccess(nil, req, nil, nil), DeepEquals, errForbidden)
// user is root (on snapd.socket)
c.Check(ac.CheckAccess(nil, req, &daemon.Ucrednet{Uid: 0, Pid: 100, Socket: dirs.SnapdSocket}, nil), IsNil)
// snap request (as root) with relevant connected plug (on snapd-snap.socket)
c.Check(ac.CheckAccess(s.d, req, &daemon.Ucrednet{Uid: 0, Pid: 1042, Socket: dirs.SnapSocket}, nil), IsNil)
// snap request without relevant connected plug (on snapd-snap.socket)
c.Check(ac.CheckAccess(s.d, req, &daemon.Ucrednet{Uid: 0, Pid: 42, Socket: dirs.SnapSocket}, user), DeepEquals, errForbidden)
// polkit is checked for snaps with connected plug
called := 0
restore = daemon.MockCheckPolkitAction(func(r *http.Request, u *daemon.Ucrednet, action string) *daemon.APIError {
called++
c.Check(r, Equals, req)
c.Check(action, Equals, "action-id")
return nil
})
defer restore()
// regular user (on snapd.socket)
c.Check(ac.CheckAccess(nil, req, &daemon.Ucrednet{Uid: 1001, Pid: 100, Socket: dirs.SnapdSocket}, nil), IsNil)
// snap request (with macaroon) with relevant connected plug (on snapd-snap.socket)
c.Check(ac.CheckAccess(s.d, req, &daemon.Ucrednet{Uid: 1001, Pid: 1042, Socket: dirs.SnapSocket}, user), IsNil)
// snap request (without macaroon) with relevant connected plug (on snapd-snap.socket)
c.Check(ac.CheckAccess(s.d, req, &daemon.Ucrednet{Uid: 1001, Pid: 1042, Socket: dirs.SnapSocket}, nil), IsNil)
c.Check(called, Equals, 3)
}
func (s *accessSuite) TestRequireInterfaceApiAccessErrorChecks(c *C) {
d := s.daemon(c)
req := &http.Request{}
// no side of the connection is specified
c.Check(daemon.RequireInterfaceApiAccessImpl(d, req, nil, daemon.InterfaceAccessReqs{}), DeepEquals,
daemon.InternalError("required connection side is unspecified"))
// check on both sides
c.Check(
daemon.RequireInterfaceApiAccessImpl(d, req, nil, daemon.InterfaceAccessReqs{
Plug: true,
Slot: true,
Interfaces: []string{"foo"},
}),
DeepEquals,
daemon.InternalError("snap cannot be specified on both sides of the connection"))
// no interfaces
c.Check(
daemon.RequireInterfaceApiAccessImpl(d, req, nil, daemon.InterfaceAccessReqs{
Plug: true,
}),
DeepEquals,
daemon.InternalError("interfaces access check, but interfaces list is empty"))
// this one actually reaches the credentials check
c.Check(
daemon.RequireInterfaceApiAccessImpl(d, req, nil, daemon.InterfaceAccessReqs{
Plug: true,
Interfaces: []string{"foo"},
}),
DeepEquals, errForbidden)
}
func reqWithAction(c *C, action string, isJSON, malformed bool) *http.Request {
rawBody := fmt.Sprintf(`{"action": "%s", "some-field": "some-data"}`, action)
if malformed {
rawBody = "}this is not json{"
}
body := strings.NewReader(rawBody)
req, err := http.NewRequest("POST", "/v2/system-volumes", body)
c.Assert(err, IsNil)
if isJSON {
req.Header.Add("Content-Type", "application/json")
}
return req
}
func (s *accessSuite) TestByActionAccess(c *C) {
byAction := map[string]daemon.AccessChecker{
"action-1": daemon.RootAccess{},
"action-2": daemon.AuthenticatedAccess{},
"action-3": daemon.OpenAccess{},
}
var ac daemon.AccessChecker = daemon.ByActionAccess{
ByAction: byAction,
Default: daemon.RootAccess{},
}
type testcase struct {
ucred daemon.Ucrednet
expectedErr map[string]*daemon.APIError
noAuth bool
notJSON bool
malformed bool
}
tcs := []testcase{
{
ucred: daemon.Ucrednet{Uid: 42, Pid: 100, Socket: dirs.SnapSocket},
noAuth: true,
expectedErr: map[string]*daemon.APIError{
"action-1": errForbidden,
"action-2": errForbidden,
"action-3": errForbidden,
"default": errForbidden,
},
},
{
ucred: daemon.Ucrednet{Uid: 42, Pid: 100, Socket: dirs.SnapdSocket},
expectedErr: map[string]*daemon.APIError{
"action-1": errForbidden,
"default": errForbidden,
},
},
{
ucred: daemon.Ucrednet{Uid: 42, Pid: 100, Socket: dirs.SnapdSocket},
noAuth: true,
expectedErr: map[string]*daemon.APIError{
"action-1": errForbidden,
"action-2": errUnauthorized,
"default": errForbidden,
},
},
{
ucred: daemon.Ucrednet{Uid: 42, Pid: 100, Socket: dirs.SnapdSocket},
notJSON: true,
expectedErr: map[string]*daemon.APIError{
"action-1": daemon.BadRequest(`unexpected content type: ""`),
"action-2": daemon.BadRequest(`unexpected content type: ""`),
"action-3": daemon.BadRequest(`unexpected content type: ""`),
"default": daemon.BadRequest(`unexpected content type: ""`),
},
},
{
ucred: daemon.Ucrednet{Uid: 42, Pid: 100, Socket: dirs.SnapdSocket},
// content type is JSON, but it's invalid
malformed: true,
expectedErr: map[string]*daemon.APIError{
"action-1": daemon.BadRequest("invalid character '}' looking for beginning of value"),
"action-2": daemon.BadRequest("invalid character '}' looking for beginning of value"),
"action-3": daemon.BadRequest("invalid character '}' looking for beginning of value"),
"default": daemon.BadRequest("invalid character '}' looking for beginning of value"),
},
},
{
ucred: daemon.Ucrednet{Uid: 0, Pid: 100, Socket: dirs.SnapdSocket},
noAuth: true,
},
{
ucred: daemon.Ucrednet{Uid: 0, Pid: 100, Socket: dirs.SnapdSocket},
notJSON: true,
noAuth: true,
expectedErr: map[string]*daemon.APIError{
"action-1": daemon.BadRequest(`unexpected content type: ""`),
"action-2": daemon.BadRequest(`unexpected content type: ""`),
"action-3": daemon.BadRequest(`unexpected content type: ""`),
"default": daemon.BadRequest(`unexpected content type: ""`),
},
},
{
ucred: daemon.Ucrednet{Uid: 0, Pid: 100, Socket: dirs.SnapdSocket},
noAuth: true,
// content type is JSON, but it's invalid
malformed: true,
expectedErr: map[string]*daemon.APIError{
"action-1": daemon.BadRequest("invalid character '}' looking for beginning of value"),
"action-2": daemon.BadRequest("invalid character '}' looking for beginning of value"),
"action-3": daemon.BadRequest("invalid character '}' looking for beginning of value"),
"default": daemon.BadRequest("invalid character '}' looking for beginning of value"),
},
},
}
for idx, tc := range tcs {
user := &auth.UserState{}
if tc.noAuth {
user = nil
}
for action := range byAction {
cmt := Commentf("sub-test tcs[%d] failed for action %q", idx, action)
err := ac.CheckAccess(nil, reqWithAction(c, action, !tc.notJSON, tc.malformed), &tc.ucred, user)
if expectedErr := tc.expectedErr[action]; err != nil {
c.Check(err, DeepEquals, expectedErr, cmt)
} else {
c.Check(err, IsNil, cmt)
}
}
cmt := Commentf("sub-test tcs[%d] failed for default action", idx)
err := ac.CheckAccess(nil, reqWithAction(c, "default", !tc.notJSON, tc.malformed), &tc.ucred, user)
if expectedErr := tc.expectedErr["default"]; err != nil {
c.Check(err, DeepEquals, expectedErr, cmt)
} else {
c.Check(err, IsNil, cmt)
}
}
}
func (s *accessSuite) TestByActionAccessDefaultMustBeRoot(c *C) {
type testcase struct {
ac daemon.AccessChecker
canBeDefault bool
}
tcs := []testcase{
{ac: daemon.RootAccess{}, canBeDefault: true},
{ac: daemon.InterfaceRootAccess{Interfaces: []string{"iface"}}, canBeDefault: true},
{ac: daemon.InterfaceProviderRootAccess{Interfaces: []string{"iface"}}, canBeDefault: true},
{ac: daemon.OpenAccess{}, canBeDefault: false},
{ac: daemon.AuthenticatedAccess{}, canBeDefault: false},
{ac: daemon.SnapAccess{}, canBeDefault: false},
{ac: daemon.InterfaceOpenAccess{Interfaces: []string{"iface"}}, canBeDefault: false},
{ac: daemon.InterfaceAuthenticatedAccess{Interfaces: []string{"iface"}}, canBeDefault: false},
{ac: daemon.ByActionAccess{Default: daemon.RootAccess{}}, canBeDefault: false},
}
for _, tc := range tcs {
body := strings.NewReader(`{"action": "unknown"}`)
req, err := http.NewRequest("POST", "/v2/system-volumes", body)
c.Assert(err, IsNil)
req.Header.Add("Content-Type", "application/json")
ac := daemon.ByActionAccess{Default: tc.ac}
ucred := daemon.Ucrednet{Uid: 0, Pid: 100, Socket: dirs.SnapdSocket}
err = ac.CheckAccess(nil, req, &ucred, nil)
if tc.canBeDefault {
c.Assert(err, IsNil)
} else {
c.Assert(err, DeepEquals, daemon.InternalError("internal error: default access checker must have root-level access: got %T", tc.ac))
}
}
}
func (s *accessSuite) TestByActionAccessLargeJSON(c *C) {
body := strings.NewReader(fmt.Sprintf(`{"action": "%s"}`, strings.Repeat("a", 4*1024*1024)))
req, err := http.NewRequest("POST", "/v2/system-volumes", body)
c.Assert(err, IsNil)
req.Header.Add("Content-Type", "application/json")
ac := daemon.ByActionAccess{Default: daemon.RootAccess{}}
ucred := daemon.Ucrednet{Uid: 0, Pid: 100, Socket: dirs.SnapdSocket}
err = ac.CheckAccess(nil, req, &ucred, nil)
c.Assert(err, DeepEquals, daemon.BadRequest("body size limit exceeded"))
}
func (s *accessSuite) TestByActionAccessDataAfterJOSN(c *C) {
body := strings.NewReader(fmt.Sprintf(`{"action": "some-action"} data`))
req, err := http.NewRequest("POST", "/v2/system-volumes", body)
c.Assert(err, IsNil)
req.Header.Add("Content-Type", "application/json")
ac := daemon.ByActionAccess{Default: daemon.RootAccess{}}
ucred := daemon.Ucrednet{Uid: 0, Pid: 100, Socket: dirs.SnapdSocket}
err = ac.CheckAccess(nil, req, &ucred, nil)
c.Assert(err, DeepEquals, daemon.BadRequest("unexpected data after request body"))
}
|