Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ jobs:
{
echo '### Coverage (src/, unit + stress tests)'
echo '```'
lcov --list build/coverage.info --ignore-errors unused,empty
lcov --list build/coverage.info --ignore-errors unused,empty,inconsistent
echo '```'
} >> "$GITHUB_STEP_SUMMARY"

Expand Down
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ endif()

# Coverage report (requires ENABLE_COVERAGE=ON, lcov and genhtml)
# Usage: make coverage (after running ctest / integration tests)
# gcov 15 can record a lambda's lines as hit but the lambda itself as not;
# lcov 2 treats that as fatal ("inconsistent"), so it is only a warning here.
if (ENABLE_COVERAGE)
find_program(LCOV_EXECUTABLE lcov)
find_program(GENHTML_EXECUTABLE genhtml)
Expand All @@ -135,9 +137,11 @@ if (ENABLE_COVERAGE)
COMMAND ${LCOV_EXECUTABLE} --capture --directory ${PROJECT_BINARY_DIR}
--output-file ${PROJECT_BINARY_DIR}/coverage.info
--include ${PROJECT_SOURCE_DIR}/src/*
--ignore-errors mismatch,unused,empty,gcov
--ignore-errors mismatch,unused,empty,gcov,inconsistent
COMMAND ${LCOV_EXECUTABLE} --list ${PROJECT_BINARY_DIR}/coverage.info
--ignore-errors inconsistent
COMMAND ${GENHTML_EXECUTABLE} ${PROJECT_BINARY_DIR}/coverage.info
--ignore-errors inconsistent
--output-directory ${PROJECT_BINARY_DIR}/coverage-html
COMMAND ${CMAKE_COMMAND} -E echo "Coverage report: ${PROJECT_BINARY_DIR}/coverage-html/index.html"
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
Expand Down
4 changes: 4 additions & 0 deletions cmake/ProjectOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ function(ss_project_options target)
# it when they are rewritten.
target_compile_options(${target} PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions -fno-rtti -fno-threadsafe-statics>)
# libstdc++ 15 enables its assertions at -O0, and they call
# __glibcxx_assert_fail from the C++ runtime, which the programs and
# libshadowsocks-c must not need (scripts/check_cxx_runtime.py).
target_compile_definitions(${target} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:_GLIBCXX_NO_ASSERTIONS>)
# GCC's C++ -Wall adds -Wsign-compare and -Wclass-memaccess, which C's
# -Wall does not have. Modules converted from C keep the warning set
# they were written against; ss_strict_warnings() turns both back on.
Expand Down
20 changes: 20 additions & 0 deletions doc/ss-manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ stat: {"8001":11370}

There is no way to reset the traffic statistics, unless you remove the port and add it again

\section ss_manager_security SECURITY

The protocol carries no credentials, so ss-manager decides who may use it by
where commands come from:

- A UNIX domain socket (recommended) is created with mode 0600, owned by the
user ss-manager runs as (or by the `-a` user its servers run as).
- An IP manager address must be a loopback one; any other is refused. On
Linux a datagram is obeyed only if its source socket belongs to the same
user, root, or the `-a` user; elsewhere any local user can reach a
loopback address, so prefer the UNIX socket there.
- A per-server `plugin` names a program to run and is accepted only over the
UNIX socket.

The working directory (`-D`) holds each server's configuration, with its
password, in owner-only files that are deleted when the port is removed; it
must be owned by the user running ss-manager and writable by nobody else.
`add` answers once the new ss-server is serving (or has failed), within ten
seconds.

\section ss_manager_example EXAMPLE

To use ss-manager(1), First start it and specify necessary information.
Expand Down
79 changes: 54 additions & 25 deletions src/acl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,24 @@ static struct ss_ipset outbound_block_list_ipv4;
static struct ss_ipset outbound_block_list_ipv6;
static rule_list_t outbound_block_list_rules;

/*
* Parse an address for ACL lookups. An IPv4-mapped IPv6 address
* (::ffff:a.b.c.d) reaches the IPv4 host on a dual-stack socket, so it is
* matched against the IPv4 lists.
*/
static int
acl_ip_init(struct ss_ip *addr, const char *text)
{
static const uint8_t mapped_prefix[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
int err = ss_ip_init(addr, text);
if (!err && addr->version == 6 && memcmp(addr->bytes, mapped_prefix, sizeof(mapped_prefix)) == 0) {
memmove(addr->bytes, addr->bytes + 12, 4);
addr->version = 4;
return 1;
}
return err;
}

static int
parse_addr_cidr(const char *str, char *host, size_t host_len, int *cidr)
{
Expand Down Expand Up @@ -116,14 +134,8 @@ init_acl(const char *path)
return -1;
}

// initialize ipset

ss_ipset_clear(&white_list_ipv4);
ss_ipset_clear(&white_list_ipv6);
ss_ipset_clear(&black_list_ipv4);
ss_ipset_clear(&black_list_ipv6);
ss_ipset_clear(&outbound_block_list_ipv4);
ss_ipset_clear(&outbound_block_list_ipv6);
// Start from nothing: a reload must not keep the previous lists or mode.
free_acl();


struct ss_ipset *list_ipv4 = &black_list_ipv4;
Expand Down Expand Up @@ -203,16 +215,33 @@ init_acl(const char *path)
int cidr;
if (parse_addr_cidr(line, host, sizeof(host), &cidr) == -1) {
LOGE("invalid ACL entry: %s", line);
continue;
fclose(f);
free_acl();
return -1;
}

struct ss_ip addr;
int err = ss_ip_init(&addr, host);
int err = acl_ip_init(&addr, host);
if (err == 1) {
// ::ffff:a.b.c.d/N is the IPv4 prefix a.b.c.d/(N-96)
if (cidr >= 0 && cidr < 96) {
LOGE("invalid ACL IPv4-mapped CIDR: %s", line);
fclose(f);
free_acl();
return -1;
}
if (cidr >= 96) {
cidr -= 96;
}
err = 0;
}
if (!err) {
if (addr.version == 4) {
if (cidr > 32 || cidr == -2) {
LOGE("invalid ACL IPv4 CIDR: %s", line);
continue;
fclose(f);
free_acl();
return -1;
}
if (cidr >= 0) {
ss_ipset_assign(list_ipv4, addr.bytes, 32, (unsigned)cidr, true);
Expand All @@ -222,7 +251,9 @@ init_acl(const char *path)
} else if (addr.version == 6) {
if (cidr > 128 || cidr == -2) {
LOGE("invalid ACL IPv6 CIDR: %s", line);
continue;
fclose(f);
free_acl();
return -1;
}
if (cidr >= 0) {
ss_ipset_assign(list_ipv6, addr.bytes, 128, (unsigned)cidr, true);
Expand All @@ -232,21 +263,17 @@ init_acl(const char *path)
}
} else {
rule_t *rule = new_rule();
if (rule == NULL) {
continue;
}
int status = accept_rule_arg(rule, line);
int status = rule != NULL ? accept_rule_arg(rule, line) : -1;
if (status == 1) status = init_rule(rule);
if (status < 0) {
if (status != 1) {
// A rule that cannot be used must not silently drop
// out of a block list.
LOGE("invalid ACL rule: %s", line);
free_rule(rule);
fclose(f);
free_acl();
return -1;
}
if (status != 1) {
free_rule(rule);
continue;
}
add_rule(rules, rule);
}
}
Expand Down Expand Up @@ -281,6 +308,8 @@ free_acl(void)
free_rules(&black_list_rules);
free_rules(&white_list_rules);
free_rules(&outbound_block_list_rules);

acl_mode = BLACK_LIST;
}

int
Expand All @@ -299,7 +328,7 @@ acl_match_host(const char *host)
{
struct ss_ip addr;
int ret = 0;
int err = ss_ip_init(&addr, host);
int err = acl_ip_init(&addr, host) < 0 ? -1 : 0;

if (err) {
int host_len = strlen(host);
Expand Down Expand Up @@ -329,7 +358,7 @@ int
acl_add_ip(const char *ip)
{
struct ss_ip addr;
int err = ss_ip_init(&addr, ip);
int err = acl_ip_init(&addr, ip) < 0 ? -1 : 0;
if (err) {
return -1;
}
Expand All @@ -347,7 +376,7 @@ int
acl_remove_ip(const char *ip)
{
struct ss_ip addr;
int err = ss_ip_init(&addr, ip);
int err = acl_ip_init(&addr, ip) < 0 ? -1 : 0;
if (err) {
return -1;
}
Expand All @@ -370,7 +399,7 @@ outbound_block_match_host(const char *host)
{
struct ss_ip addr;
int ret = 0;
int err = ss_ip_init(&addr, host);
int err = acl_ip_init(&addr, host) < 0 ? -1 : 0;

if (err) {
int host_len = strlen(host);
Expand Down
20 changes: 15 additions & 5 deletions src/android.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,26 @@ protect_socket(int fd)
return -1;
}

char ret = 0;

if (recv(sock, &ret, 1, 0) == -1) {
ERROR("[android] recv");
// The VPN service answers one byte: 0 once the socket is protected.
// Anything else, or no answer, leaves the socket routed into the VPN.
char ret = 1;
ssize_t n = recv(sock, &ret, 1, 0);
if (n != 1) {
if (n == -1) {
ERROR("[android] recv");
} else {
LOGE("[android] protect_path closed without an answer");
}
close(sock);
return -1;
}

close(sock);
return ret;
if (ret != 0) {
LOGE("[android] failed to protect socket %d", fd);
return -1;
}
return 0;
}

extern char *stat_path;
Expand Down
2 changes: 2 additions & 0 deletions src/core/buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class FixedBuffer {

constexpr std::size_t capacity() const noexcept { return N; }
constexpr std::size_t size() const noexcept { return len_; }
// All of the storage, whatever is in use, for wiping secrets.
Bytes storage() noexcept { return { data_.data(), N }; }
constexpr bool empty() const noexcept { return len_ == 0; }
constexpr bool full() const noexcept { return idx_ + len_ == N; }

Expand Down
14 changes: 11 additions & 3 deletions src/crypto/aead.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ class HmacSha1 {

bool ppbloom_seen(void *, std::span<const std::uint8_t> salt) noexcept
{
return ppbloom_check(salt.data(), static_cast<int>(salt.size())) == 1;
// An unusable filter (-1) counts as seen: replay protection fails closed.
return ppbloom_check(salt.data(), static_cast<int>(salt.size())) != 0;
}

void ppbloom_remember(void *, std::span<const std::uint8_t> salt) noexcept
Expand Down Expand Up @@ -177,6 +178,7 @@ Status hkdf_sha1(std::span<const std::uint8_t> salt, std::span<const std::uint8_
expand.update({ &counter, 1 });
if (!expand.finish(t)) {
sodium_memzero(prk, sizeof(prk));
sodium_memzero(t, sizeof(t));
return fail(Errc::crypto);
}
t_len = kHash;
Expand Down Expand Up @@ -290,6 +292,11 @@ Status AeadTcpEncryptor::init(const MasterKey &master, ReplayFilter filter, bool
salt_sent_ = false;
nonce_.fill(0);
randombytes_buf(salt_.data(), master.spec.key_len);
if (printable_salt && master.spec.key_len < 32) {
// Up to 12 constrained bytes leave >= 238 bits of a 32-byte salt
// but only ~111 of a 16-byte one: not offered there.
return fail(Errc::config);
}
if (printable_salt) {
// Vary the constrained span without adding a delimiter or marker.
std::size_t prefix = 6 + randombytes_uniform(7);
Expand Down Expand Up @@ -418,8 +425,9 @@ Result<AeadTcpDecryptor::Step> AeadTcpDecryptor::decrypt(std::span<const std::ui
failed_ = true;
return fail(Errc::crypto);
}
std::size_t payload = load16_be(len) & kMaxChunk;
if (payload == 0) {
// The two top bits are reserved (SIP004): reject, don't mask.
std::size_t payload = load16_be(len);
if (payload == 0 || payload > kMaxChunk) {
failed_ = true;
return fail(Errc::protocol);
}
Expand Down
12 changes: 12 additions & 0 deletions src/crypto/aead.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ struct MasterKey {
AeadSpec spec;
std::array<std::uint8_t, kMaxKey> bytes{};

MasterKey() noexcept = default;
MasterKey(const AeadSpec &s, const std::array<std::uint8_t, kMaxKey> &b) noexcept : spec(s), bytes(b) {}
MasterKey(const MasterKey &) noexcept = default;
MasterKey &operator=(const MasterKey &) noexcept = default;
~MasterKey() { sodium_memzero(bytes.data(), bytes.size()); }

std::span<const std::uint8_t> key() const noexcept { return { bytes.data(), spec.key_len }; }

// EVP_BytesToKey(MD5) from a password, as the C implementation does.
Expand Down Expand Up @@ -168,6 +174,12 @@ class AeadTcpEncryptor {

class AeadTcpDecryptor {
public:
AeadTcpDecryptor() noexcept = default;
AeadTcpDecryptor(const AeadTcpDecryptor &) = delete;
AeadTcpDecryptor &operator=(const AeadTcpDecryptor &) = delete;
// The reassembly buffer held decrypted payload.
~AeadTcpDecryptor() { sodium_memzero(buffer_.storage().data(), buffer_.storage().size()); }

Status init(const MasterKey &master, ReplayFilter filter) noexcept;

struct Step {
Expand Down
22 changes: 18 additions & 4 deletions src/crypto/aead2022.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ std::size_t socks_addr_len(std::span<const std::uint8_t> buf) noexcept
return 0;
}
std::size_t len = 0;
switch (buf[0] & 0x0F) {
switch (buf[0]) { // exactly 1, 3 or 4, as the SOCKS parser reads it
case 1:
len = 1 + 4;
break;
Expand Down Expand Up @@ -222,7 +222,9 @@ bool SaltPool::check_and_add(std::span<const std::uint8_t> salt, std::uint64_t n
return false;
}
if (seen_.full()) {
(void)seen_.pop_oldest();
// Every remembered salt is still inside the replay window: forgetting
// one would let its request be replayed, so refuse the new one.
return false;
}
return seen_.emplace(*key, now).has_value();
}
Expand Down Expand Up @@ -672,12 +674,24 @@ Result<std::size_t> udp2022_accept(Role role, UdpSession2022 &session, const Udp
}
std::size_t pad = load16_be(body + o);
o += kLenSize;
if (o + pad > n) {
if (pad > k2022MaxPad || o + pad > n) {
return fail(Errc::protocol);
}
o += pad;

if (!session.rx_ready || session.rx_id != packet.session_id) {
if (session.rx_ready && session.rx_id != packet.session_id) {
// Keep the current window as the previous one, or bring the previous
// one back for a late packet of that session.
bool back = session.prev_ready && session.prev_id == packet.session_id;
std::swap(session.rx_id, session.prev_id);
std::swap(session.rx_highest, session.prev_highest);
std::swap(session.rx_window, session.prev_window);
std::swap(session.rx_ready, session.prev_ready);
if (!back) {
session.rx_ready = false;
}
}
if (!session.rx_ready) {
window_reset(session, packet.session_id, packet.packet_id);
} else if (!window_check(session, packet.packet_id)) {
return fail(Errc::crypto); // replayed
Expand Down
Loading
Loading