fix(security): LMTP allow list and a warning for an open LMTP port

- Add lmtp.allow: clients outside the list are dropped before the greeting (TCP only)
- Validate the list at load, naming the bad entry
- Warn at startup when LMTP is reachable beyond the machine and no list is set
- Stop publishing the LMTP port in the README and the example config; add Security notes
This commit is contained in:
tgiachi 2026-11-09 20:20:35 +01:00
commit 064e8beed8
6 changed files with 244 additions and 4 deletions

View file

@ -36,7 +36,7 @@ Three listeners, each configurable:
| Listener | Default | Purpose |
|----------|---------|---------|
| `lmtp` | `127.0.0.1:2424` | mail from Postfix |
| `lmtp` | `127.0.0.1:2424` | mail from Postfix; **no authentication**, see Security notes |
| `api` | `127.0.0.1:8080` | admin REST API, bearer token |
| `web` | `127.0.0.1:8081` | public archive and unsubscribe pages (`""` disables) |
@ -133,10 +133,11 @@ docker build -t parsimony .
docker run -d --name parsimony --read-only --cap-drop ALL \
-v parsimony-data:/data \
-v $PWD/parsimony.yaml:/etc/parsimony/parsimony.yaml:ro \
-e PARSIMONY_API_TOKEN=... -p 2424:2424 -p 8080:8080 -p 8081:8081 parsimony
-e PARSIMONY_API_TOKEN=... -p 127.0.0.1:8080:8080 -p 8081:8081 parsimony
```
Use `db: /data/parsimony.db` in the config. A new named volume gets the right ownership by itself;
Use `db: /data/parsimony.db` in the config. Do not publish the LMTP port (2424): Postfix reaches it over a Docker network
shared with the container, and `lmtp.allow` should list that network's range (see Security notes). A new named volume gets the right ownership by itself;
for a bind mount, make the host directory writable by uid 65532.
## Delivery and the queue
@ -194,6 +195,22 @@ transport_maps = hash:/etc/postfix/transport
lists.example.org lmtp:inet:127.0.0.1:2424
```
## Security notes
- **LMTP has no authentication.** Whoever can connect to it can send mail as any member. Keep it on `127.0.0.1`, on a
private network, or on a unix socket, and list the clients that may connect:
```yaml
lmtp:
addr: 0.0.0.0:2424
allow: [172.16.0.0/12] # CIDR ranges or single addresses; other clients are dropped before the greeting
```
The daemon logs a warning at startup when LMTP is reachable beyond the machine and `lmtp.allow` is empty.
`allow` does not apply to unix sockets: use the permissions of the socket file.
- The API and the admin UI use one bearer token and no TLS: put them behind a reverse proxy with TLS and keep the `api`
listener private.
## Licence
Parsimony is free software, released under the GNU General Public License, version 3 or (at your option) any later

View file

@ -29,6 +29,56 @@ import (
// nowFunc is the clock of the store (queue schedules, token expiry). Tests move it.
var nowFunc = time.Now
// allowListener drops connections from clients that are not in the allow list,
// before the LMTP greeting: LMTP has no authentication of its own.
type allowListener struct {
net.Listener
nets []*net.IPNet
}
func (l allowListener) Accept() (net.Conn, error) {
for {
c, err := l.Listener.Accept()
if err != nil {
return nil, err
}
if l.allowed(c.RemoteAddr()) {
return c, nil
}
log.Printf("lmtp: refused a connection from %s (not in lmtp.allow)", c.RemoteAddr())
c.Close()
}
}
func (l allowListener) allowed(addr net.Addr) bool {
tcp, ok := addr.(*net.TCPAddr)
if !ok {
return false
}
for _, n := range l.nets {
if n.Contains(tcp.IP) {
return true
}
}
return false
}
// lmtpIsOpen reports whether the LMTP listener is reachable beyond this machine.
func lmtpIsOpen(network, addr string) bool {
if network != "tcp" && network != "tcp4" && network != "tcp6" {
return false // a unix socket is protected by the permissions of its file
}
host, _, err := net.SplitHostPort(addr)
if err != nil {
return true
}
if host == "localhost" {
return false
}
ip := net.ParseIP(host)
return ip == nil || !ip.IsLoopback()
}
// App is a running daemon.
type App struct {
// LMTPAddr and APIAddr are the addresses actually bound.
@ -101,6 +151,12 @@ func Start(cfg config.Config, apiToken string) (*App, error) {
}
a.LMTPAddr = lmtpLn.Addr().String()
a.lns = append(a.lns, lmtpLn)
serveLn := net.Listener(lmtpLn)
if allow := cfg.LMTPAllowed(); len(allow) > 0 && (cfg.LMTP.Network == "tcp" || cfg.LMTP.Network == "tcp4" || cfg.LMTP.Network == "tcp6") {
serveLn = allowListener{Listener: lmtpLn, nets: allow}
} else if lmtpIsOpen(cfg.LMTP.Network, cfg.LMTP.Addr) {
log.Printf("warning: LMTP on %s accepts any client and has no authentication, so anyone who can reach it can post as any member: set lmtp.allow or listen on 127.0.0.1", cfg.LMTP.Addr)
}
var apiLn net.Listener
if apiToken != "" {
@ -144,7 +200,7 @@ func Start(cfg config.Config, apiToken string) (*App, error) {
a.wg.Add(1)
go func() {
defer a.wg.Done()
if err := a.lmtp.Serve(lmtpLn); err != nil && !errors.Is(err, smtp.ErrServerClosed) && !errors.Is(err, net.ErrClosed) {
if err := a.lmtp.Serve(serveLn); err != nil && !errors.Is(err, smtp.ErrServerClosed) && !errors.Is(err, net.ErrClosed) {
log.Printf("lmtp: %v", err)
}
}()

View file

@ -1,6 +1,7 @@
package app
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
@ -8,6 +9,7 @@ import (
"encoding/pem"
"errors"
"io"
"log"
"net"
"net/http"
"os"
@ -637,3 +639,92 @@ func TestStartFailsOnAnUnusableDKIMKey(t *testing.T) {
}
}
}
// greeting dials LMTP and returns the first line the server says, or an error if it hangs up.
func greeting(network, addr string) (string, error) {
conn, err := net.DialTimeout(network, addr, 2*time.Second)
if err != nil {
return "", err
}
defer conn.Close()
conn.SetReadDeadline(time.Now().Add(2 * time.Second))
buf := make([]byte, 128)
n, err := conn.Read(buf)
return string(buf[:n]), err
}
func TestLMTPAllowListAdmitsOnlyListedClients(t *testing.T) {
relayAddr, _ := startSink(t)
allowed := testConfig(t, relayAddr)
allowed.LMTP.Allow = []string{"10.255.0.0/16", "127.0.0.0/8"}
a, err := Start(allowed, "tok")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { a.Close() })
if g, err := greeting("tcp", a.LMTPAddr); err != nil || !strings.HasPrefix(g, "220") {
t.Fatalf("a listed client is served: %q %v", g, err)
}
denied := testConfig(t, relayAddr)
denied.LMTP.Allow = []string{"10.255.0.0/16"}
b, err := Start(denied, "tok")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { b.Close() })
if g, err := greeting("tcp", b.LMTPAddr); err == nil || g != "" {
t.Fatalf("a client outside the list must be dropped before the greeting: %q %v", g, err)
}
// The daemon keeps working for the allowed ones after refusing someone.
if g, err := greeting("tcp", a.LMTPAddr); err != nil || !strings.HasPrefix(g, "220") {
t.Fatalf("still serving: %q %v", g, err)
}
}
func TestLMTPAllowListDoesNotApplyToUnixSockets(t *testing.T) {
relayAddr, _ := startSink(t)
cfg := testConfig(t, relayAddr)
cfg.LMTP.Network = "unix"
cfg.LMTP.Addr = filepath.Join(t.TempDir(), "lmtp.sock")
cfg.LMTP.Allow = []string{"10.0.0.0/8"} // meaningless for a socket: access is the file's permissions
a, err := Start(cfg, "tok")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { a.Close() })
if g, err := greeting("unix", cfg.LMTP.Addr); err != nil || !strings.HasPrefix(g, "220") {
t.Fatalf("unix socket: %q %v", g, err)
}
}
func TestStartWarnsAboutAnOpenLMTPPort(t *testing.T) {
relayAddr, _ := startSink(t)
for name, tc := range map[string]struct {
addr string
allow []string
warn bool
}{
"all interfaces, no list": {"0.0.0.0:0", nil, true},
"empty host, no list": {":0", nil, true},
"all interfaces, listed": {"0.0.0.0:0", []string{"127.0.0.0/8"}, false},
"loopback": {"127.0.0.1:0", nil, false},
"localhost": {"localhost:0", nil, false},
} {
var buf bytes.Buffer
log.SetOutput(&buf)
cfg := testConfig(t, relayAddr)
cfg.LMTP.Addr = tc.addr
cfg.LMTP.Allow = tc.allow
a, err := Start(cfg, "tok")
log.SetOutput(os.Stderr)
if err != nil {
t.Fatalf("%s: %v", name, err)
}
a.Close()
if got := strings.Contains(buf.String(), "lmtp.allow"); got != tc.warn {
t.Errorf("%s: warning = %v, want %v\n%s", name, got, tc.warn, buf.String())
}
}
}

View file

@ -4,6 +4,7 @@ package config
import (
"bytes"
"fmt"
"net"
"os"
"strings"
@ -17,6 +18,9 @@ type Config struct {
LMTP struct {
Network string `yaml:"network"` // tcp or unix
Addr string `yaml:"addr"`
// Allow limits which clients may connect (CIDR ranges or single addresses, TCP only).
// LMTP has no authentication: anyone who can connect can post as any member.
Allow []string `yaml:"allow"`
} `yaml:"lmtp"`
API struct {
Listen string `yaml:"listen"` // HTTP listen address; the bearer token comes from PARSIMONY_API_TOKEN
@ -39,7 +43,41 @@ type DKIMKey struct {
KeyFile string `yaml:"key_file"` // PEM private key
}
// LMTPAllowed returns the parsed lmtp.allow list (empty: every client is accepted).
func (c Config) LMTPAllowed() []*net.IPNet {
nets := make([]*net.IPNet, 0, len(c.LMTP.Allow))
for _, a := range c.LMTP.Allow {
if n, err := parseNet(a); err == nil {
nets = append(nets, n)
}
}
return nets
}
// parseNet accepts "10.0.0.0/8" or a single address ("10.1.2.3", "::1").
func parseNet(s string) (*net.IPNet, error) {
s = strings.TrimSpace(s)
if strings.Contains(s, "/") {
_, n, err := net.ParseCIDR(s)
return n, err
}
ip := net.ParseIP(s)
if ip == nil {
return nil, fmt.Errorf("not an IP address or CIDR range: %q", s)
}
bits := 128
if ip4 := ip.To4(); ip4 != nil {
ip, bits = ip4, 32
}
return &net.IPNet{IP: ip, Mask: net.CIDRMask(bits, bits)}, nil
}
func (c Config) validate() error {
for i, a := range c.LMTP.Allow {
if _, err := parseNet(a); err != nil {
return fmt.Errorf("lmtp.allow[%d]: %w", i, err)
}
}
seen := map[string]bool{}
for i, k := range c.DKIM {
switch {

View file

@ -1,6 +1,7 @@
package config
import (
"net"
"os"
"path/filepath"
"strings"
@ -89,3 +90,39 @@ func TestLoadRejectsIncompleteOrDuplicateDKIMKeys(t *testing.T) {
t.Fatalf("the error names the entry: %v", err)
}
}
func TestLoadLMTPAllowList(t *testing.T) {
c, err := Load(write(t, "lmtp:\n allow: [127.0.0.0/8, 172.16.0.0/12, 10.1.2.3, \"::1\", \"fd00::/8\"]\n"))
if err != nil {
t.Fatal(err)
}
nets := c.LMTPAllowed()
if len(nets) != 5 {
t.Fatalf("nets: %v", nets)
}
for ip, want := range map[string]bool{
"127.0.0.1": true, "172.20.0.5": true, "10.1.2.3": true, "10.1.2.4": false, "::1": true, "fd00::5": true, "8.8.8.8": false,
} {
got := false
for _, n := range nets {
if n.Contains(net.ParseIP(ip)) {
got = true
}
}
if got != want {
t.Errorf("%s allowed = %v, want %v", ip, got, want)
}
}
if c, _ := Load(write(t, "{}\n")); len(c.LMTPAllowed()) != 0 {
t.Fatal("no allow list by default")
}
}
func TestLoadRejectsABadLMTPAllowEntry(t *testing.T) {
for _, bad := range []string{"not-an-ip", "10.0.0.0/33", "10.0.0/8", "", "10.0.0.1/"} {
_, err := Load(write(t, "lmtp:\n allow: [\"127.0.0.1\", \""+bad+"\"]\n"))
if err == nil || !strings.Contains(err.Error(), "lmtp.allow[1]") {
t.Errorf("%q: want an error naming the entry, got %v", bad, err)
}
}
}

View file

@ -3,6 +3,7 @@ base_url: https://lists.example.org
lmtp:
network: tcp
addr: 0.0.0.0:2424
allow: [172.16.0.0/12] # LMTP has no authentication: list who may connect (here: the Docker networks)
relay:
addr: postfix:25
concurrency: 4 # simultaneous deliveries