Skip to content

Commit

Permalink
Merge branch 'develop' into pledge
Browse files Browse the repository at this point in the history
  • Loading branch information
neilalexander authored Dec 12, 2024
2 parents a6fcdfc + 83ec58a commit f2c863a
Show file tree
Hide file tree
Showing 25 changed files with 562 additions and 340 deletions.
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,31 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- in case of vulnerabilities.
-->

## [0.5.10] - 2024-11-24

### Added

* The `getPeers` admin endpoint will now report the current transmit/receive rate for each given peer
* The `getMulticastInterfaces` admin endpoint now reports much more useful information about each interface, rather than just a list of interface names

### Changed

* Minor tweaks to the routing algorithm:
* The next-hop selection will now prefer shorter paths when the costed distance is otherwise equal, tiebreaking on peering uptime to fall back to more stable paths
* Link cost calculations have been smoothed out, making the costs less sensitive to sudden spikes in latency
* Reusable name lookup and peer connection logic across different peering types for more consistent behaviour
* Some comments in the configuration file have been revised for clarity
* Upgrade dependencies

### Fixed

* Nodes with `IfName` set to `none` will now correctly respond to debug RPC requests
* The admin socket will now be created reliably before dropping privileges with `-user`
* Clear supplementary groups when providing a group ID as well as a user ID to `-user`
* SOCKS and WebSocket peerings should now use the correct source interface when specified in `InterfacePeers`
* `Peers` and `InterfacePeers` addresses that are obviously invalid (such as unspecified or multicast addresses) will now be correctly ignored
* Listeners should now shut down correctly, which should resolve issues where multicast listeners for specific interfaces would not come back up or would log errors

## [0.5.9] - 2024-10-19

### Added
Expand Down
94 changes: 32 additions & 62 deletions cmd/yggdrasil/chuser_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,83 +4,53 @@
package main

import (
"errors"
"fmt"
"math"
osuser "os/user"
"os/user"
"strconv"
"strings"
"syscall"

"golang.org/x/sys/unix"
)

func chuser(user string) error {
group := ""
if i := strings.IndexByte(user, ':'); i >= 0 {
user, group = user[:i], user[i+1:]
}
func chuser(input string) error {
givenUser, givenGroup, _ := strings.Cut(input, ":")

u := (*osuser.User)(nil)
g := (*osuser.Group)(nil)
var (
err error
usr *user.User
grp *user.Group
uid, gid int
)

if user != "" {
if _, err := strconv.ParseUint(user, 10, 32); err == nil {
u, err = osuser.LookupId(user)
if err != nil {
return fmt.Errorf("failed to lookup user by id %q: %v", user, err)
}
} else {
u, err = osuser.Lookup(user)
if err != nil {
return fmt.Errorf("failed to lookup user by name %q: %v", user, err)
}
if usr, err = user.LookupId(givenUser); err != nil {
if usr, err = user.Lookup(givenUser); err != nil {
return err
}
}
if group != "" {
if _, err := strconv.ParseUint(group, 10, 32); err == nil {
g, err = osuser.LookupGroupId(group)
if err != nil {
return fmt.Errorf("failed to lookup group by id %q: %v", user, err)
}
} else {
g, err = osuser.LookupGroup(group)
if err != nil {
return fmt.Errorf("failed to lookup group by name %q: %v", user, err)
}
}
if uid, err = strconv.Atoi(usr.Uid); err != nil {
return err
}

if g != nil {
gid, _ := strconv.ParseUint(g.Gid, 10, 32)
var err error
if gid < math.MaxInt {
err = syscall.Setgid(int(gid))
} else {
err = errors.New("gid too big")
if givenGroup != "" {
if grp, err = user.LookupGroupId(givenGroup); err != nil {
if grp, err = user.LookupGroup(givenGroup); err != nil {
return err
}
}

if err != nil {
return fmt.Errorf("failed to setgid %d: %v", gid, err)
}
} else if u != nil {
gid, _ := strconv.ParseUint(u.Gid, 10, 32)
err := syscall.Setgid(int(uint32(gid)))
if err != nil {
return fmt.Errorf("failed to setgid %d: %v", gid, err)
}
gid, _ = strconv.Atoi(grp.Gid)
} else {
gid, _ = strconv.Atoi(usr.Gid)
}

if u != nil {
uid, _ := strconv.ParseUint(u.Uid, 10, 32)
var err error
if uid < math.MaxInt {
err = syscall.Setuid(int(uid))
} else {
err = errors.New("uid too big")
}

if err != nil {
return fmt.Errorf("failed to setuid %d: %v", uid, err)
}
if err := unix.Setgroups([]int{gid}); err != nil {
return fmt.Errorf("setgroups: %d: %v", gid, err)
}
if err := unix.Setgid(gid); err != nil {
return fmt.Errorf("setgid: %d: %v", gid, err)
}
if err := unix.Setuid(uid); err != nil {
return fmt.Errorf("setuid: %d: %v", uid, err)
}

return nil
Expand Down
80 changes: 80 additions & 0 deletions cmd/yggdrasil/chuser_unix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris

package main

import (
"testing"
"os/user"
)

// Usernames must not contain a number sign.
func TestEmptyString (t *testing.T) {
if chuser("") == nil {
t.Fatal("the empty string is not a valid user")
}
}

// Either omit delimiter and group, or omit both.
func TestEmptyGroup (t *testing.T) {
if chuser("0:") == nil {
t.Fatal("the empty group is not allowed")
}
}

// Either user only or user and group.
func TestGroupOnly (t *testing.T) {
if chuser(":0") == nil {
t.Fatal("group only is not allowed")
}
}

// Usenames must not contain the number sign.
func TestInvalidUsername (t *testing.T) {
const username = "#user"
if chuser(username) == nil {
t.Fatalf("'%s' is not a valid username", username)
}
}

// User IDs must be non-negative.
func TestInvalidUserid (t *testing.T) {
if chuser("-1") == nil {
t.Fatal("User ID cannot be negative")
}
}

// Change to the current user by ID.
func TestCurrentUserid (t *testing.T) {
usr, err := user.Current()
if err != nil {
t.Fatal(err)
}

if usr.Uid != "0" {
t.Skip("setgroups(2): Only the superuser may set new groups.")
}

if err = chuser(usr.Uid); err != nil {
t.Fatal(err)
}
}

// Change to a common user by name.
func TestCommonUsername (t *testing.T) {
usr, err := user.Current()
if err != nil {
t.Fatal(err)
}

if usr.Uid != "0" {
t.Skip("setgroups(2): Only the superuser may set new groups.")
}

if err := chuser("nobody"); err != nil {
if _, ok := err.(user.UnknownUserError); ok {
t.Skip(err)
}
t.Fatal(err)
}
}
23 changes: 23 additions & 0 deletions cmd/yggdrasil/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"strings"
"syscall"

"suah.dev/protect"

"github.com/gologme/log"
gsyslog "github.com/hashicorp/go-syslog"
"github.com/hjson/hjson-go/v4"
Expand All @@ -39,6 +41,20 @@ type node struct {

// The main function is responsible for configuring and starting Yggdrasil.
func main() {
// Not all operations are coverable with pledge(2), so immediately
// limit file system access with unveil(2), effectively preventing
// "proc exec" promises right from the start:
//
// - read arbitrary config file
// - create/write arbitrary log file
// - read/write/chmod/remove admin socket, if at all
if err := protect.Unveil("/", "rwc"); err != nil {
panic(fmt.Sprintf("unveil: / rwc: %v", err))
}
if err := protect.UnveilBlock(); err != nil {
panic(fmt.Sprintf("unveil: %v", err))
}

genconf := flag.Bool("genconf", false, "print a new config to stdout")
useconf := flag.Bool("useconf", false, "read HJSON/JSON config from stdin")
useconffile := flag.String("useconffile", "", "read HJSON/JSON config from specified file path")
Expand Down Expand Up @@ -191,9 +207,16 @@ func main() {

// Set up the Yggdrasil node itself.
{
iprange := net.IPNet{
IP: net.ParseIP("200::"),
Mask: net.CIDRMask(7, 128),
}
options := []core.SetupOption{
core.NodeInfo(cfg.NodeInfo),
core.NodeInfoPrivacy(cfg.NodeInfoPrivacy),
core.PeerFilter(func(ip net.IP) bool {
return !iprange.Contains(ip)
}),
}
for _, addr := range cfg.Listen {
options = append(options, core.ListenAddress(addr))
Expand Down
28 changes: 24 additions & 4 deletions cmd/yggdrasilctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ func run() int {
if err := json.Unmarshal(recv.Response, &resp); err != nil {
panic(err)
}
table.SetHeader([]string{"URI", "State", "Dir", "IP Address", "Uptime", "RTT", "RX", "TX", "Pr", "Cost", "Last Error"})
table.SetHeader([]string{"URI", "State", "Dir", "IP Address", "Uptime", "RTT", "RX", "TX", "Down", "Up", "Pr", "Cost", "Last Error"})
for _, peer := range resp.Peers {
state, lasterr, dir, rtt := "Up", "-", "Out", "-"
state, lasterr, dir, rtt, rxr, txr := "Up", "-", "Out", "-", "-", "-"
if !peer.Up {
state, lasterr = "Down", fmt.Sprintf("%s ago: %s", peer.LastErrorTime.Round(time.Second), peer.LastError)
} else if rttms := float64(peer.Latency.Microseconds()) / 1000; rttms > 0 {
Expand All @@ -202,6 +202,12 @@ func run() int {
uri.RawQuery = ""
uristring = uri.String()
}
if peer.RXRate > 0 {
rxr = peer.RXRate.String() + "/s"
}
if peer.TXRate > 0 {
txr = peer.TXRate.String() + "/s"
}
table.Append([]string{
uristring,
state,
Expand All @@ -211,6 +217,8 @@ func run() int {
rtt,
peer.RXBytes.String(),
peer.TXBytes.String(),
rxr,
txr,
fmt.Sprintf("%d", peer.Priority),
fmt.Sprintf("%d", peer.Cost),
lasterr,
Expand Down Expand Up @@ -285,9 +293,21 @@ func run() int {
if err := json.Unmarshal(recv.Response, &resp); err != nil {
panic(err)
}
table.SetHeader([]string{"Interface"})
fmtBool := func(b bool) string {
if b {
return "Yes"
}
return "-"
}
table.SetHeader([]string{"Name", "Listen Address", "Beacon", "Listen", "Password"})
for _, p := range resp.Interfaces {
table.Append([]string{p})
table.Append([]string{
p.Name,
p.Address,
fmtBool(p.Beacon),
fmtBool(p.Listen),
fmtBool(p.Password),
})
}
table.Render()

Expand Down
10 changes: 9 additions & 1 deletion contrib/mobile/mobile.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,15 @@ func (m *Yggdrasil) StartJSON(configjson []byte) error {
}
// Set up the Yggdrasil node itself.
{
options := []core.SetupOption{}
iprange := net.IPNet{
IP: net.ParseIP("200::"),
Mask: net.CIDRMask(7, 128),
}
options := []core.SetupOption{
core.PeerFilter(func(ip net.IP) bool {
return !iprange.Contains(ip)
}),
}
for _, peer := range m.config.Peers {
options = append(options, core.Peer{URI: peer})
}
Expand Down
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/yggdrasil-network/yggdrasil-go
go 1.21

require (
github.com/Arceliar/ironwood v0.0.0-20241016082300-f6fb9da97a17
github.com/Arceliar/ironwood v0.0.0-20241210120540-9deb08d9f8f9
github.com/Arceliar/phony v0.0.0-20220903101357-530938a4b13d
github.com/cheggaaa/pb/v3 v3.1.5
github.com/coder/websocket v1.8.12
Expand All @@ -14,10 +14,10 @@ require (
github.com/quic-go/quic-go v0.46.0
github.com/vishvananda/netlink v1.3.0
github.com/wlynxg/anet v0.0.5
golang.org/x/crypto v0.28.0
golang.org/x/net v0.30.0
golang.org/x/sys v0.26.0
golang.org/x/text v0.19.0
golang.org/x/crypto v0.29.0
golang.org/x/net v0.31.0
golang.org/x/sys v0.27.0
golang.org/x/text v0.20.0
golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2
golang.zx2c4.com/wireguard v0.0.0-20231211153847-12269c276173
golang.zx2c4.com/wireguard/windows v0.5.3
Expand All @@ -34,7 +34,7 @@ require (
go.uber.org/mock v0.4.0 // indirect
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sync v0.9.0 // indirect
golang.org/x/tools v0.23.0 // indirect
)

Expand Down
Loading

0 comments on commit f2c863a

Please sign in to comment.