Raspberry Pi Mini-NAS

An off-the-shelf NAS device is convenient but there are also drawbacks. A normal PC for absolute control over the software suits me more. Right now, with ridiculously inflated prices for components, getting either would be really a huge Schnapsidee (←sorry, there is no adequate English word for this).

The factual unavailability of affordable PC components doesn’t stop me from starting the process of setting up the software on a Raspberry Pi 4b. This has two advantages: I learn how to do it right now and I get a working backup target that can run 24/7 as it consumes very little energy.

Full disclosure: Since nobody would be willing to assist me and normal search engines suck at finding information nowadays, I resorted to LLMs for gathering some bits and pieces.

I set everything up on my own though! (And had to correct several blunders the LLMs had in their output. So, I am using my brain and not just posting “AI-slop” here)

0 – Disclaimer

As usual: This is NOT a tutorial (and therefore doesn’t contain step-by-step instructions). I am not a security expert and this is not an advice to build the same I did! Don’t even think of holding me responsible if you choose to do it anyway. Never allow a Samba server to be accessible from the internet.
I’m just sharing my thoughts and experiences, nothing more, nothing less.

1 – Hardware

Not much so say here:

  • Raspberry Pi 4b, early 2GB model, original PSU
  • Cheap case, heat sinks on all chips and active CPU cooling with a tiny fan
  • 256GB Transcend micro-SD ←With support for trim command!
  • USB flash drive, for now 64GB, to be replaced with a bigger one

I’ve had this lying around pretty much since the Raspi 4b had been released. To my shame I must confess it had been unused most of the time (likely less than 10 hours of active usage). The costs for these components had been very low. Nowadays a Raspberry Pi 5 is MUCH more expensive.

Raspberry Pi 4b in semi-transparent case with a little fan.
Original PSU, 256GB Transcend micro-SD and a silver Intenso branded small USB flash drive are lying next to it

2 – Software

Not much to say here either:

I decided for just using Raspberry Pi OS Lite (currently based on Debian 13). No GUI at all. GUI is wasteful for the scarce resources and furthermore useless attack surface on a tiny server running in headless mode.

Then I installed the following with apt: luks, ufw, usbguard, samba, wsdd2

3 – Setting Things Up

Now to the actual work that I had to do. Just installing Samba and creating some shares doesn’t fulfill the minimum security requirements. To be able to store data encryption is mandatory.

3.1 – SSH

Administration shall be done via SSH only. To protect the SSH server I disabled a few things explicitly in /etc/ssh/sshd_config
PermitRootLogin no
PasswordAuthentication no
PermitEmptyPasswords no
X11Forwarding no

A single element whitelist allows the user “sinchen” to connect with public key authentication only.
AllowUsers sinchen

3.2 – Encryption

Sadly Raspberry Pi OS defaults to expanding the main partition to the size of the currently used SD card. I was lazy, turned off the Pi and inserted the Transcend medium into my main workstation to shrink the partition with gparted to make enough room for a well-sized LUKS-partition.

Trade-off is that it cannot unlock and mount the data partitions unattended. Since it will run 24/7 and only reboot on power outage and kernel update I don’t see much of an issue here. A little script automates unlocking the two partitions (just enter passphrase)
Having a fully unattended boot process and working storage encryption are more or less mutually exclusive. Look up BitLocker bypasses if you believe otherwise. Raspis don’t have a TPM anyway. Automatic network based unlock is not without its own drawbacks and requires another (well-protected) machine running 24/7. This isn’t the case for my network.

sinchen@Raspi4:~ $ lsblk
NAME            MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINTS
loop0             7:0    0   1,8G  0 loop   
sda               8:0    1  58,6G  0 disk   
└─sda1            8:1    1  58,6G  0 part   
 └─64G_USB      253:1    0  58,6G  0 crypt /BACKUP
mmcblk0         179:0    0 235,5G  0 disk   
├─mmcblk0p1     179:1    0   512M  0 part  /boot/firmware
├─mmcblk0p2     179:2    0  43,9G  0 part  /
└─mmcblk0p3     179:3    0 191,1G  0 part   
 └─datenpartition 253:0    0   191G  0 crypt /data
zram0           254:0    0   1,8G  0 disk  [SWAP]

No full-disk encryption means no protection for meta-data and log files! I’ve not yet looked into Raspberry Pi implementation of Secure Boot allowing some kind of FDE with automatic unlock as it ultimately requires burning a key into OTP… ←Deep rabbit hole!

3.3 Samba

3.3.1 Global

Samba defaults have been improved at some point. A clear case of better, not good. While the horrible and insecure SMB1 protocol is inactive if not explicitly allowed, the same cannot be said about SMB2. I tried it with a Windows 7 VM.
SMB v2.1, was happily accepted by my unfinished server. Samba tries to be helpful and friendly to legacy devices and negotiates what version is supported by a client. This way a malicious machine can simply downgrade the protocol. I’m usually not a fan of arrogance. In this case I had to teach Samba to be more arrogant and tell clients: “Either you speak SMB3.1.1 with me, or f… off!”

Not sure which of these settings are needed and which are redundant because they represent current defaults, these are the ones I chose in the global section:

disable netbios = yes
smb ports = 445
hosts allow = 192.168.178.0/24
hosts deny = ALL
server min protocol = SMB3_11
client min protocol = SMB3_11
smb encrypt = required
server signing = mandatory
client signing = mandatory
map to guest = never
usershare allow guests = no

This does work: Windows 7 throws nonsensical errors now and CPU load easily maxes out one core when copying a large file (which suggests encryption is on since – to my knowledge – a Pi 4b has no hardware accelerated AES ←If true: What were they thinking!?). Without the hardened settings CPU load was lower (only LUKS encryption) on copying a file from Windows 7.

Is that a problem? No, because the micro-SD maxes out at about 30MB/s of sustained writing speed. Reading is faster, but I generally was unable to get it to full speed (gigabit Ethernet) with any settings.

3.3.2 Shares

This little NAS is supposed to be a central exchange point for small files between client computers. To limit the damage a compromised client (ransomware) can cause each PC will get its own user account and its own folder read/write while allowing read-only access to the other machines’ shares. Samba and the Linux file permissions enforce this. All shares are this format:

[Latitude3420]
   comment = Dell Latitude 3420
   path = /data/latitude3420
   valid users = @sambausers
   read only = yes
   write list = latitude3420
   create mask = 0640
   directory mask = 2750

As you can see the machines have standard user accounts and share membership in the custom group “sambausers”. As there is absolutely no reason for these accounts to ever login into Linux, I set their shell to /usr/sbin/nologin→This does not impact Samba access.

d rwxr-s--- latitude3420  sambausers  latitude3420
d rwxr-s--- phenom945     sambausers  phenom945
d rwxr-s--- sunshine      sambausers  sunshine
d rwxr-s--- win10old      sambausers  win10old
d rwxr-s--- windows11test sambausers  windows11test

3.5 – UFW

Some very basic firewall rules are easily set with ufw. Limiting attempts to ssh, accepting Samba port 445 only from IP-range of my LAN. A few more ports for making it discoverable (wsdd2). I have yet to check if all of these are strictly necessary. Theoretically nothing speaks against manually addressing shares with the (static) IP address – this is likely also more secure as it omits an additional service. But… I find it more elegant if a machine advertises its presence.

All in all UFW was done in five minutes.

sinchen@Raspi4:/data $ sudo ufw status
Status: active
To                 Action      From
--                 ------      ----
22/tcp             LIMIT       192.168.178.0/24           
445/tcp            ALLOW       192.168.178.0/24           
3702               ALLOW       192.168.178.0/24           
5355               ALLOW       192.168.178.0/24           
1900               ALLOW       192.168.178.0/24    

3.5 – USBGuard

3.5.1 – Local Attacks

Main weakness of a file server is that it cannot put data at rest. One solution is using (additional) client-side encryption as if this was some not trustworthy cloud storage. That adds a lot of complexity and is highly inconvenient, at least for a Samba server. Surely someone will suggest a totally obvious, modern, everybody-and-their-dog-uses-this alternative that I have never heard of. For now I’ll stick with SMB as it is readily available everywhere without installing anything.

Short of a real solution I had to come up with mitigation ideas. The question of possible DMA attack vectors on a Raspberry Pi makes my face turn into a question mark. I do not even know what the GPIO pins allow doing when nothing is set up.

For PCs this topic is documented fairly well. DMA attacks are possible through PCI(e), FireWire, Thunderbolt and possibly more interfaces. Possible mitigation options against cold boot and DMA attacks can also be easily found (this goes way too far here)

USB devices don’t get direct DMA, which is good. That doesn’t mean there is no attack surface.

3.5.2 – 🦆Do You Like Ducks?🦆

🦆Generally I like ducks. Nice animals. Calming to watch them swim.🦆

Has Sinchen finally and fully lost her mind? Changing the topic right in the middle of a blog entry and talking about ducks??

Well, I wasn’t actually thinking of living waterbirds. I was thinking of tools in the style of USB Rubber Ducky. This can inject keystrokes, huge scripts.
Then there is the possibility of attaching a flash memory stick for extracting data. Of course this needs a kind of local vulnerability.

I still see no reason to accept any unknown USB devices. That is where USBGuard comes in. You define rules. Which USB devices are allowed – and on which port? Any devices not meeting the rules are blocked → The Rubber Ducky can quack all it wants, but no one’s listening.

3.5.3 – Emergency Shutdown

To be honest ignoring device insertion is not good enough. Once USBGuard notices that there is an unauthorized device connected, the machine has now recorded an event representing a local attack! In that case I want the Pi to do the following as quickly as possible (order matters):

  • Stop Samba
  • Unmount data partitions
  • Purge encryption keys
  • shutdown -h now

Right now I have some crude logic with a custom service. It works: Follow journal, if any usbguard message with “block” appears wait a moment and verify there is a blocked device listed before initiating panic reaction. This is needed because USBguard defaults to block even with whitelisted devices allowing them afterwards if a rule matches. I won’t share the embarrassing script.

Yes, I know this this needs improvement as it gives an attacking device tiny amount of time before triggering a shutdown. Racing against the attacker is a bad idea. How much better is your protection right now?

3.6 – Snapshots

Not yet implemented: Resistance against ransomware attacks in the form of snapshots on the USB flash drive. I’m planning usage of rsnapshot to create local copies that are not accessible to Samba (users) and therefore out of range for Windows clients.

4 – Security Evaluation

Can I call this a secure system? Hard to answer. It depends on what department of security is meant.

Secure backup?
NO! This solution alone doesn’t meet the 3-2-1 rule, let alone the 3-2-1-1-0 rule. It is an additional puzzle piece that protects data from certain threats while providing convenient data sharing for the home network. This is not my only backup. My already existing backup concept should fulfill 3-2-1-1-0, but that is off-topic here.

Secure shares?
To my knowledge about as good as SMB can get.

Secure operating system?
Mediocre at best. A standard Debian installation is not really a fortress. Samba must not be accessible from the internet. I disabled unused crap such as Bluetooth and WiFi services, locked down local attack vectors and hardened the only remote administration tool – sshd. This is basic, not secure.
Strict account separation and strict ACLs do their part, but this is not a silver bullet either. There is no mandatory access control enabled. Once I have backed up this installation I will try to get rid of certain “living off the land binaries” like curl and wget and probably more. AppArmor should definitely confine the Samba daemon. I have no idea why neither AppArmor nor SELinux are active. → Work to do!

5 – Final Thoughts

This turned out to be a satisfactory project implemented in three days without much prior knowledge. The Raspberry Pi 4b will now run 24/7 and improve my home network with very low running costs.

Raspberry Pi now installed in network distribution box inside the wall.
This also contains a standard network switch with several colorful Ethernet cables connected.

Having gone through this I could easily do the same on a desktop PC equipped with a bunch of enterprise HDDs in RAID-5 configuration. In fact this is my long-term goal.
The Samba server would then be running in a virtual machine and the shares would live on a btrfs partition with integrated snapshot functionality. With Xen as hypervisor using IOMMU and with runtime memory encryption (modern CPUs) it would be a huge step up in security.