50
May/June 2017 TM TM FreeBSD’s Firewall Feast GamingBSD Toward Oblivious Sandboxing with Capsicum Cadets: Blending Tracing and Security Whats Going On with CloudABI? pfSense & Security JOURNAL TM

May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

May/June 2017

TM

TM

• FreeBSD’s Firewall Feast • GamingBSD • Toward Oblivious Sandboxing with Capsicum• Cadets: Blending Tracing and Security• Whats Going On with CloudABI? • pfSense & Security

JOURNALTM

Page 2: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,
Page 3: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

TM

May/June 2017 1

SECURITY

3 FoundationLetter Shh, don’t tell anyone.By George Neville-Neil

42 New FacesIn this installment, thespotlight is on EugeneGrosbein, a longtimecontributor who transi-tioned to a ports com-mitter in March 2017.By Dru Lavigne

44 Book ReviewDesigning BSD Root-kits: An Introductionto Kernel Hacking byJoseph Kong. Reviewedby Steven Kreuzer

46 svn UpdateThis installment high-lights recent improve-ments designed to keepyou and your data safe.By Steven Kreuzer

48 EventsCalendarBy Dru Lavigne

May/June 2017

C O L U M N S &D E PA R T M E N T S

Capsicum, a principled and coherent design for softwarecompartmentalization, has taken recent strides toward a new security model. By Jonathan Anderson, Stanley Godfrey, Robert N. M. Watson

As part of a research project, CADETSis adding new security primitives to FreeBSD as well asleveraging existing primitives to produce an operating system with maximum transparency. By JonathanAnderson, George V. Neville-Neil, Arun Thomas,Robert N. M. Watson

CADETS: Blending Tracing & Security on FreeBSD

pfSense® software provides users with the main featuresoffered by most commercial firewalls, but with the primaryadvantages of costing less and being open source. By James Dekker, Renato Botelho, Luiz Otavio O. Souza

Toward Oblivious Sand-boxing with Capsicum

pfSense® and Security

Please be in your most para-noid state when reading this article—ready to wear thetwo hats of security, the black one and the white one. By Roberto Fernández

Gaming BSDSDBS

GAMING

FreeBSD is famous for all sorts of fantastic features. It’ssomewhat infamous, however, for having three differentfirewalls. By Michael W Lucas

FreeBSD’s Firewall Feast

4

As CloudABI is part of FreeBSD 11and most new features have already been merged into 11-STABLE, CloudABI has become an easy-to-use tool for creating secure and testable software. By Ed Schouten

What’s Going On withCloudABI?

12

18

6

32

2626

18

32

12

6

Page 4: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

Donate to the Foundation!

Support FreeBSD

You already know that FreeBSD is an internationally recognized leader in providing a high-performance, secure, and stable operating system. It �s because of you. Your donations have a direct impact on the Project.

Please consider making a gift to support FreeBSD for the coming year. It�s only with your help that we can continue and increase our support to make FreeBSD the high-performance, secure, and reliable OS you know and love!

Your investment will help:

Funding Projects to Advance FreeBSD

Increasing Our FreeBSD Advocacy and

Providing Additional ConferenceResources and Travel Grants

Continued Development of the FreeBSDJournal

Protecting FreeBSD IP and ProvidingLegal Support to the Project

Purchasing Hardware to Build andImprove FreeBSD Project Infrastructure

Making a donation is quick and easy. freebsdfoundation.org/donate

®

®

Page 5: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

FreeBSD Journal (ISBN: 978-0-615-88479-0) is published 6 times ayear (January/February, March/April, May/June, July/August,September/October, November/December).Published by the FreeBSD Foundation, 5757 Central Ave., Suite 201, Boulder, CO 80301ph: 720/207-5142 • fax: 720/222-2350email: [email protected] © 2017 by FreeBSD Foundation. All rights reserved.

This magazine may not be reproduced in whole or in part withoutwritten permission from the publisher.

LETTERfrom the Board

Publisher •

Editor-at-Large •

Copy Editor •Art Director •

Office Administrator •

Advertising Sales •

Walter [email protected]

James [email protected]

Annaliese Jakimides

Dianne M. [email protected]

Michael Davis [email protected]

Walter [email protected] 888/290-9469

P O B O X 4 0 8 , B E L F A S T , M A I N E 0 4 9 1 5

George Neville-NeilEditor in Chief of the FreeBSD Journal

Director of the FreeBSD Foundation

May/June 2017 3

Shh, don’t tell anyone.

John Baldwin •

Brooks Davis •

Bryan Drewery •

Justin Gibbs •

Daichi Goto •

Joseph Kong •

Steven Kreuzer •Dru Lavigne •

Michael W Lucas •Ed Maste •

Kirk McKusick •

George V. Neville-Neil •

Hiroki Sato •

Benedict Reuschling •

Robert N. M. Watson •

J O U R N A L

Member of the FreeBSD Core Teamand Co-Chair of FreeBSD JournalEditorial Board

Senior Software Engineer at SRIInternational, Visiting Industrial Fellowat University of Cambridge, and pastmember of the FreeBSD Core Team

Senior Software Engineer at EMC Isilon,member of FreeBSD Portmgr Team,and FreeBSD Committer

Founder and President of the FreeBSD Foundation and a Senior Software Architect at Spectra Logic Corporation

Director at BSD Consulting Inc.(Tokyo)

Senior Software Engineer at EMC andauthor of FreeBSD Device Drivers

Member of the FreeBSD Ports Team

Director of the FreeBSD Foundation,Chair of the BSD Certification Group,and author of BSD Hacks

Author of Absolute FreeBSD

Director of Project Development,FreeBSD Foundation

Director of the FreeBSDFoundation and lead author ofThe Design and Implementationbook series

Director of the FreeBSD Foundation,Chair of FreeBSD Journal Editorial Board, and coauthor of The Design and Implementation of the FreeBSD Operating System

Director of the FreeBSDFoundation, Chair of AsiaBSDCon, member of the FreeBSDCore Team, and Assistant Professor at Tokyo Institute of Technology

Vice President of the FreeBSDFoundation and a FreeBSDDocumentation Committer

Director of the FreeBSD Foundation,Founder of the TrustedBSD Project,and University Senior Lecturer at the University of Cambridge

E d i t o r i a l B o a r d

TM

S&W PUBLISHING LLC

It’s a secret. We’ve compiled an entire issue on “security”! Well,actually, please do not keep this issue a secret as it contains anexcellent roundup of interesting technologies that help makeFreeBSD more secure as well as ways in which FreeBSD itself is

applied to the problems of secure systems.We kick off the issue with Michael Lucas’s piece on the

Firewalls of FreeBSD. FreeBSD contains the PF, IPFilter, and IPFWfirewalls, systems that ostensibly implement the same features,but have several interesting differences. Next is an article on theCADETS research project. With CADETS, we’re blending tracing—via DTrace—with the audit system and other components ofFreeBSD to produce a platform based on the idea that havingtransparency—i.e., who does what to whom—on a computerallows us to build a more secure system overall. Then JonathanAnderson, Stanley Godfrey, and Robert N. M. Watson take us on ajourney through the sandbox, via their article on Capsicum, thenative, capability-based isolation technology that is present inFreeBSD. Capsicum resurrected the concept of Capability Systems,first researched in the 1970s. Capabilities were once consideredtoo computationally expensive for practical systems, but the workdone by Robert N. M. Watson in his PhD thesis showed that acapability-based system could be built for Unix-like systems thatwas both practical and efficient. James Dekker, Renato Botelho,and Luiz Otavio O. Souza guide us through the main security fea-tures of pfSense in their article on this popular, FreeBSD-derived,open-source firewall product. FreeBSD can be found in many inter-esting environments, including devices that implement games ofchance. In Europe, you’ll see these games in corner bars and othervenues. Roberto Fernández tries to induce a bit more paranoiathan is typical by explaining what it takes to secure a gamingdevice with FreeBSD. We round out this issue with an update onCloudABI, by Ed Schouten. A tool that’s part of FreeBSD, CloudABIhelps create secure software that is easier to test and verify.

Steven Kreuzer also highlights the security theme, reviewingJoseph Kong’s book Designing BSD Rootkits: An Introduction toKernel Hacking, as well as covering all that’s new in the FreeBSDsource tree in his svn update column. Through Dru Lavigne’s NewFaces, we’re introduced to Eugene Grosbein, a longtime contribu-tor who transitioned to a ports committer in March. And don’tforget to check out the BSD-related events we should considerputting on our calendars.

It’s time now to sit back, put on your tinfoil hat, and enjoyreading this latest issue of the FreeBSD Journal.

Page 6: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

4 FreeBSD Journal

S E ET E X TO N L Y

reeBSD is famous for all sorts of fantasticfeatures, such as ZFS, jails, bhyve virtual-ization, and the Ports Collection. It’ssomewhat infamous, however, for having

three different firewalls: PF, IPFilter, and IPFW.Where did all these firewalls come from, andwhy are they all still in the system?

The IT industry has repeatedly abused,stretched, and tormented the word firewall to fitall sorts of different products. When someoneuses firewall, ask them exactly what they’re talk-ing about. Do they mean a caching HTTP proxylike Squid or Varnish? A generic proxy likerelayd? Or a TCP/IP packet filter?

All of FreeBSD’s firewalls are packet filters.They control which TCP/IP addresses and portscan connect to the host. If your FreeBSD hostpasses packets between interfaces, then the fire-wall controls which traffic gets forwarded, which

gets silently dropped, and which is sent back tothe source with a letter of complaint. A packet-forwarding packet filter is the original firewall.

The firewalls all have a common core featureset considered the minimum for a modern pack-et filter. They can track the state of a TCP/IP con-nection and permit traffic based on existing con-nections. They can all return resets or silentlydrop connections. All can manage non-routableaddresses and perform network address transla-tion. They all work with lists of rules defininghow to respond to traffic from different IPaddresses and network ports. Incoming packetsare compared to the list of rules until they arepermitted or rejected.

The firewalls have their own unique features,however. How do you choose between them?

IPFW is the oldest FreeBSD packet filter, dat-ing from 1995. IPFW maintains its rules in a

FBy Michae l W LucasFreeBSD’s Firewall Feast

Page 7: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

numbered list. You add a rule to an existing rulesetby giving it a rule number. Rule numbers rangefrom 1 to 65534. IPFW rules are very dynamic, andcan be altered programmatically. Numbered rulesdo impose some limits, however. Every time Idesign an IPFW ruleset, I eventually wind up havingto renumber the rules because, somehow, I needto cram too many rules in between two otherrules. This is a limitation of my imagination whencreating my ruleset, though, not anylimitation in IPFW.

The most interesting unique featurein IPFW is dummynet, which lets youselectively degrade traffic. Why wouldyou want to make a connectionworse? Well, for one thing, it’s kindof fun to simulate an ADSL link to themoon. But the ability to imposeexcess latency and bandwidth limita-tions on a connection can save aglobal organization days or weeks oftime and thousands of dollars in developer travelexpenses. More than once, I’ve set up an IPFWbox to simulate a connection from the other sideof the world, or with multiple profiles to simulateseveral different countries. A user in South Koreawith a gigabit Internet line will have a very differ-ent experience than a user who’s closer but hasless bandwidth. Dummynet lets you simulate theirexperience locally.

IPFW also has the highest performance of anyFreeBSD firewall, although that only becomesapparent at tens of gigabits per second.

IPFilter, or IPF, is a platform-independent firewall,and came to FreeBSD in the late 1990s. You canuse IPFilter on FreeBSD, Solaris, SunOS, HP-UX,NetBSD, OpenBSD, and Linux. It has a configura-tion syntax similar to PF. If you must run one fire-wall across multiple operating systems, IPFilter isfor you.

IPFilter is not in active development, but it canblock, permit, and translate packets. It’s largelyfeature-complete.

PF is the newest FreeBSD firewall, and is quitepopular among younger sysadmins. It originatedwith the OpenBSD project, as their replacement forIPFilter. It’s quite popular among younger sysad-mins who didn’t grow up with IPFW.

PF is perhaps the most popular FreeBSD firewall.PF has a simpler configuration syntax than IPFW.While you can alter the ruleset programmatically,

you must configure the ruleset to permit suchalterations. While mailing list archives discuss occa-sional problems, such as handling IPv6 fragmenta-tion, those issues were solved years ago.

While FreeBSD’s PF originated with OpenBSD,that import happened several years ago. FreeBSD’sPF has diverged from the original import, andOpenBSD’s PF has continued its natural evolution.Chances are that the two will continue to diverge.

When you’re studying PF configuration, be surethat you use documentation relevant for FreeBSD,not current OpenBSD documentation. The chancesof FreeBSD taking a new import of OpenBSD’s PFare very slim. PF is also incompatible with the vim-age virtual networking stack used by jails.

All this information is nice, but which shouldyou choose? That depends on your environment.Most sysadmins with small servers will find PF’ssimplicity a win.

If you want one firewall software across multipleUnix-like operating systems, use IPFilter.

If you need to pass tens of gigabits per second,simulate a bad connection, have unlimited abilityto programmatically alter rules, or requireadvanced jail networking, use IPFW.

If you already have one deployed, of course,keep using it. They all work fine for the essentialfirewall task of filtering packets. Combined with anapplication proxy or cache such as Squid, relayd, orvarnish, FreeBSD can go head-to-head with anycommercial firewall vendor and win. •

MICHAEL W LUCAS is the author of several books on FreeBSD, includingAbsolute FreeBSD and the FreeBSDMastery series. Learn more atwww.michaelwlucas.com.

All this information is nice, but which

should you choose?

May/June 2017 5

Page 8: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

S E ET E X TO N L Y

6 FreeBSD Journal

By James Dekker, Luiz Otavio O. Souza, and Renato Botelho

and Security

FreeBSD is an operating system well-known forits advanced networkstack and security. Itprovides thousands ofthird-party softwarepackages through theports collection, making itpossible for expert users tohave a great firewall runningwith a minimum of investment.Other users with less expertise oreven no experience with FreeBSD, and in some cases more money, prefer to run their security solutions using commercial products.

Page 9: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

May/June 2017 7

fSense® software exists in the middle ofthese two worlds. pfSense is an open-

source firewall/router distribution running ontop of FreeBSD. All configuration occurs viaan easy-to-use web GUI, and many functionsare provided using other open-source appli-cations (e.g., Unbound, Strongswan,OpenVPN, etc.). pfSense software providesusers with the main features offered by mostcommercial firewalls, but with the primaryadvantages of costing less and being opensource. Open-source software can be auditedand fixed when its behavior or security is indoubt. The open-source model also allowsanyone to fix broken code, while closedsource can only be fixed by the vendor.

pfSense software was born in 2004, as afork of m0n0wall. Since then it has beencontinuously developed and improved, mak-ing it a trusted product used by hundreds ofthousands of users around the world.Currently there are over 550,000 activeinstallations of pfSense software.

Netgate is the company behind pfSenseand employs core developers, making it pos-sible to have people working on and support-ing it every day of the year. Netgate offers24/7 support and other professional servicesto help users design their network fromscratch, review existing configurations, andmigrate environments from most knowncommercial products to pfSense software tomeet the customer’s requirements.

Improvements in New VersionsAfter a long period of being based on FreeBSD 8,pfSense software version 2.2, based on FreeBSD10.3, was released January 23, 2015. We havecontinued tracking FreeBSD closely, and as a result,the 2.3 version, released April 12, 2016, wasbased on FreeBSD 10.3, (released March 28,2016). pfSense software 2.3 brought a large num-ber of new features and improvements. The mostsignificant changes were a complete rewrite ofwebGUI using Bootstrap and the underlying sys-tem, including the base system and kernel, beingconverted entirely to FreeBSD pkg.

Having pfSense software use pkg broughtthree large benefits. First, pfSense softwareupdates were historically distributed as a mono-

lithic image that was extracted on top of the cur-rently installed version. There were no records ofany system files. Using FreeBSD pkg made it pos-sible to distribute minor upgrades easily andmade the process of testing and validating a newupgrade much faster.

Second, using pkg to upgrade every piece ofpfSense software makes it possible to detectwhether or not the kernel is being upgraded.With version 2.4, this information is used bypfSense-upgrade to decide if the system needs tobe rebooted or not. If the answer is no, it will usereroot as an alternative and the system will restartall services and be back online faster using thesame running kernel.

Finally, starting with version 2.1, pfSense usedPBIs to distribute additional packages. This was agood choice at the time because each PBI con-tained all required libraries and other binarydependencies so as to not pollute the pfSensecore with additional files. With pkg, all primaryand additional packages are built together in thesame environment using poudriere. Poudrieretakes care that dependencies are tracked correctlyand that all unnecessary files are removed when apackage is uninstalled, leaving the system clean.

The conversion of the WebGUI to Bootstrapmade the user interface more modern, easier, andintuitive, while leaving the code both more read-able and easier to extend, resulting in a renewedlevel of contributions to the project. The numberof pull requests submitted to main pfSense reposi-tory at github more than doubled from 384 to 955during 2.3 webGUI conversion time.

We continue to track FreeBSD source closely.Our next major release, pfSense software version2.4, which is in BETA as we write this article, willbe based on FreeBSD 11. With this version, 32-bitarchitecture and the nanobsd model were discon-tinued, but we now offer images for ARM plat-form, more specifically to Netgate SG-1000(uFW). The old bsdinstaller has been replaced bybsdinstall, the same installer used by FreeBSD,which brings important features such as UEFI andZFS support.

FreeBSD 11 also brings an updated 802.11stack, which will bring pfSense software numer-ous improvements when used as a WiFi client oraccess point. Appliances offered in theNetgate/pfSense store with native wireless are

Page 10: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

8 FreeBSD Journal

fully compatible with FreeBSD 11 and are idealfor applications including research and penetra-tion testing.

During 2.3 and 2.4 development, we drasticallyreduced the number of patches we carried againstthe FreeBSD source code to accommodate fea-tures required by pfSense users. Changes thatwere of interest to upstream FreeBSD users werecontributed to FreeBSD, while other patches wereremoved and pfSense was reworked to have thesame result without patching FreeBSD. As a resultof this work, pfSense software version 2.4 is cur-rently running on top of a FreeBSD tree that ismuch closer to stock. This yields the advantagethat we can now move to a new FreeBSD majorrelease more quickly than ever before. In the past,and under different leadership, the project tooktwo years to move from FreeBSD 8.1 to FreeBSD8.3 and another 20 months to move fromFreeBSD 8.3 to FreeBSD 10.1.

PackagesOne of the major benefits of pfSense is the abili-ty to install additional packages that provideextra functionality such as Snort, Suricata, andSquid. In the past, the number of available addi-tional packages became unworkable as the main-tainers of some of them lost interest, leavingsome packages to stagnate.

During the pfSense 2.3 release process, whilewe migrated from the old pfSense-packagesrepository to a FreeBSD ports-based repository,we decided to clean up and remove packagesthat have been deprecated upstream, no longerhave an active maintainer, or were never stable.

Remaining packages were converted to thenew Bootstrap model and reworked to becomeavailable as a FreeBSD pkg. Following the releaseof version 2.3, the community came together tomigrate and maintain some packages that hadbeen removed. After review, some of these pack-ages were accepted and reintroduced to the listof available packages.

Captive PortalThe Captive Portal function in pfSense softwareallows securing a network by requiring a user-name and password (or only a click-throughentered on a portal page). If authentication isused, this can be performed with native usermanagement within pfSense or an external

authentication server such as an Active Directory,LDAP or RADIUS server.

The pfSense firewall is implemented principallyby pf, one of the three packet filter implementa-tions available in FreeBSD. When Captive Portal isenabled, it uses ipfw, another packet filter imple-mentation available in FreeBSD. Until pfSensesoftware 2.3, we had a heavily modified versionof ipfw to make it possible for Captive Portal towork with multiple instances. During the moveto FreeBSD 11, due to a huge rewrite of ipfw onFreeBSD, we decided it was a good time toremove the big ipfw patch and make CaptivePortal work with native ipfw, or at least with justa few modifications.

All Captive Portal pieces that interact withipfw were reworked, and, as a result, we wereable to eliminate one big extra patch that led uscloser than stock FreeBSD. Necessary changesdone during this work will be reviewed andupstreamed as soon as 2.4 is released.

SecuritypfSense software’s main goal is to be a securityappliance, and because of that, we take it seri-ously. In 2014, we announced Security Advisoriescontaining all security-related issues that are dis-covered, along with their corresponding CVE IDs.

A significant number of security flaws relatedto the webGUI are caused by the use of HTTPGET. To mitigate this attack, vector pfSense soft-ware 2.4 has converted many parts of thewebGUI to work only via HTTP POST.

Code ReviewIn 2016, Netgate enlisted the help of InfosecGlobal to conduct a top-to-bottom, post-commitaudit of pfSense software version 2.3.2.Conducting an independent code review helpsimprove the quality of the product.

For this project, we provided Infosec Globalwith the Netgate XG-2758 1U Security GatewayAppliance with pfSense software 2.3.2 installedwith a default production configuration.

Infosec Global scores threats on a bottom-uppercentage scale, with 0% being a perfect scoreand 100% being most critical. As indicated in theaudit report, pfSense 2.3.2 scored an outstanding1%, which included concerns that were alreadymitigated during the timeframe of the auditprocess with the release of pfSense software ver-

and Security

Page 11: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

May/June 2017 9

sion 2.3.2_p1. Other issues raised do not apply tothe firmware version reviewed. The completereport is available for review at https://www.netgate.com/assets/ISG_Netgate_2016.pdf

OpenVPNWith the release of pfSense software version 2.4,OpenVPN has been upgraded to version 2.4.0.That is a significant upgrade that includes supportfor a number of new features, including supportfor AEAD ciphers such as AES-GCM. We alsoadded support for Negotiable Crypto Parameters(NCP) to control automatic cipher selectionbetween clients and servers.

Let’s EncryptIn February 2017, a new package called ACMEwas made available for releases greater than 2.3.2in Package Manager. This package interfaces withthe Let’s Encrypt project to handle the certificategeneration, validation, and renewal processes.

Let’s Encrypt is an open, free, and completelyautomated Certificate Authority from the non-profit Internet Security Research Group (ISRG).The goal of Let’s Encrypt is to encrypt the web byremoving the cost barrier and some of the techni-cal barriers that discourage server administratorsand organizations from obtaining certificates foruse on Internet servers, primarily web servers.Most browsers trust certificates from Let’sEncrypt. These certificates can be used for webservers (HTTPS), SMTP servers, IMAP/POP3 servers,and other similar roles that utilize the same typeof certificates.

Certificates from Let’s Encrypt are domain vali-dated, and this validation ensures that the systemrequesting the certificate has authority over theserver in question. This validation can be per-formed in a number of ways, such as by provingownership of the domain’s DNS records or host-ing a file on a web server for the domain.

By using a certificate from Let’s Encrypt for aweb server, including the webGUI in pfSense, thebrowser will trust the certificate and show agreen check mark, padlock, or similar indication.The connection will be encrypted without theneed for manually trusting an invalid certificate.

TranslationsWhile pfSense has always been a project of con-tributions from the community, efforts to translate

pfSense started many years ago, but were never100% complete for any language. We were alsonever able to provide a central place or goodtoolchain for contributors of translations forpfSense. For pfSense software version 2.4, wedecided to bring this subject back and addedpfSense software to the Zanata translation web-site. Once we had announced this setup, it was ashort time until we received a full translation intoSpanish, Russian, Norwegian, Chinese (Simplified,China), Chinese (Taiwan) and Bosnian, withincredible progress in German and many others. If you want to see pfSense software translated toyour native language, join us!

Contributing to UpstreamOur main upstream is FreeBSD. Both FreeBSDsource and ports repositories are the bases usedto build and run pfSense software. During thepast two years, Netgate engineers have con-tributed 155 commits to the FreeBSD src reposito-ry and 35 commits against the FreeBSD ports collection.

But FreeBSD is not our only upstream; we alsorun other software provided by third-party proj-ects such as Strongswan, OpenVPN, and dpinger.pfSense developers consistently strive to submitfixes for upstream projects every time a bug isfound or a new feature is implemented. This ispart of an ongoing effort to improve the softwarefor the entire user base.

pfSense Software in the CloudWith today’s cloud platforms, you can easilyextend an on-premise IT environment into thecloud in a manner similar to setting up and con-necting to a remote branch office. While theseservices provide excellent tools for connectingVPNs and setting up access lists, they do not pro-vide full visibility and manageability into the end-points, nor do they secure the connectivity of crit-ical cloud services.

The Netgate-provided images for pfSense soft-ware on AWS and Azure deliver advanced rout-ing, firewall, and VPN functionality for your publiccloud infrastructure at a lower cost than othersolutions. The pre-built pfSense AWS and Azureimages have features similar to both the pfSensehardware appliances and the VMware CertifiedpfSense available from Netgate.

Page 12: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

10 FreeBSD Journal

ARM Support on pfSense 2.4We had just released pfSense 2.3 when the pro-totypes of our first ARM system landed on ourdesks. It was then we knew that the ARM sup-port on FreeBSD had evolved so quickly between10 and 11, to the point where it was extremelydifficult to port many of the new features andfixes. It became clear that FreeBSD 11 would bethe only reasonable option and we knew thatmoving on was the best way to provide the opti-mal ARM experience.

Since then, Netgate has upstreamed all thecode developed, tested, and used daily on theSG-1000. A few contributions specific to the SoCused in this platform include:

• numerous platform and Ethernet fixes toincrease system stability and performance

• dual Ethernet support• management of the integrated Ethernet

switch

The introduction of a new, less forgiving ARMarchitecture to our development environmentalso helped us spot a few misbehaving sectionsof code that would have passed unnoticed on amore forgiving architecture. The outcome of fix-ing these is code correctness and better overallportability.

Today, the ARM support in pfSense provides aseamless experience for any user of this newlyintroduced architecture. Everything was carefullyworked out to provide a reliable, smooth, andincredibly intuitive experience, to the point thatyou will likely forget you are using a tiny ARMdevice (unless you manage to remember where

this little device lives—or hides—in your office).Thanks to the pfSense pkg support for

updates, the updates on the SG-1000 are blissand the whole system is updated from theWebGUI (FreeBSD kernel and base, pfSense baseand packages), which makes the management ofthese devices terrific.

The SG-1000 has significantly increased thenumber of FreeBSD ARM devices in the world.As of this writing, we are actively working onour next ARM system, a dual core ARM withmore speed and more hardware features.

Since pfSense began as a fork of m0n0wall, ithas continued to evolve into a product thatdrives the wedge deeper and deeper into theenterprise market space, while still meeting andexceeding the needs of the SMB, prosumer, andhome user. What once was a project to providean extensible and easy-to-use software productfor an embedded hardware system has growninto a product that will now run on hardwarefrom embedded ARM devices smaller than acredit card to high availability systems that cantake up 2U of rackspace and even larger clus-tered configurations. This highly versatile,increasingly stable, and secure software productwould not be possible without the massive sup-port and involvement of both the communityand the resources of Netgate. It’s only with thecombination of these that we are able to contin-ue pushing the envelope in terms of what is pos-sible for not only FreeBSD, but also, as time willtell, a return to our roots in providing an extensi-ble, high-throughput, easy-to-use software prod-uct for embedded devices. •

RENATO BOTELHO started using FreeBSD on version 3.2 after a friend gave him an installation CDand said, “Install it and never look back.” His first contributions to FreeBSD ports tree in 2004 led tohis becoming a committer the next year. Likewise, his involvement with pfSense in 2009 was soon followed by his employment at Netgate. He lives in a small city in Brazil with his wife, son, daughter,and two dogs. When he is not in front of a computer, he enjoys running, CrossFit, beer, and barbe-cues—not necessarily in that order.

JAMES DEKKER is a diehard fan of pfSense and a Production Support Analyst for Netgate. He enjoyssecuring systems and the networks that connect them. He has been working with *nix systems forsome time, primarily focused on security, networking and systems administration. He has been usingpfSense since version 2.0 and providing support to the community since 2.1. When James is notworking or breaking things in his office, he can be found on the beach, fishing, or working on thefamily ranch. He lives on the Treasure Coast in Florida with his wife, dog, and cat.

LUIZ OTAVIO O. SOUZA is a software engineer at Netgate and a FreeBSD committer.

and Security

Page 13: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

May/June 2017 11

Page 14: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

S E ET E X TO N L Y

12 FreeBSD Journal

The FreeBSD operatingsystem has been used inmany products that requirestrong security proper-ties—from storage appli-ances, to network switchesand routers as well asgaming consoles. Over the20-plus years of FreeBSD’sdevelopment, there havebeen significant additionsto the system in the areaof security.

Blending Tracing& Security on FreeBSD

CADETS

By Jonathan Anderson,George V. Neville-Neil, Arun Thomas, andRobert N. M. Watson

Page 15: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

May/June 2017 13

The Jails [1] system introduced whattoday would be considered a lightweightversion of virtualization. The MandatoryAccess Control (MAC) framework was

added in 2002 to provide fine-grained, system-wide control over what users and programs coulddo within the system [2]. The Audit subsystemadded the ability to track—on a system-call-by-sys-tem-call basis—what actions were occurring onthe system and who or what was initiating thoseactions [3]. More recently Capsicum has intro-duced capabilities into the operating system thatare the basis for application sandboxes [4].

As part of a new research project, the CausalAdaptive Distributed, and Efficient Tracing System(CADETS), we are adding new security primitivesto FreeBSD as well as leveraging existing primitivesto produce an operating system with the maxi-mum amount of transparency. Apart from thesecurity implications, having better visibility intosystems will help us to improve overall perform-ance and provide new runtime debugging tools.

In this article, we’ll cover our work with DTraceand the Audit system, which form the core com-ponents of the work, and talk about the chal-lenges of using DTrace as an always on tracing system to track security and other events.

Starting at the BeginningOne of the main components of FreeBSD we areexploiting in CADETS is DTrace. Originally devel-oped for Sun’s Solaris operating system in the earlyyears of the 21st century [5], DTrace was meant tosolve the following problem: most software sys-tems have some sort of logging framework, whichusually depends on the ubiquitous printf func-tion and formats text and sends it to some form of console.

printf("Hello world!");

All C programmers know the code shownabove and every programmer knows the equiva-lent idiom in their own language, whether it’sPython, Rust, Go, or PHP. There are a few prob-lems with using print statements for logging sys-tems. The first problem is that print statementshave a high performance overhead. If you’ve everwondered about how much work is done on theprogrammer’s behalf by printf then see BrooksDavis’s “Everything you ever wanted to know

about ‘hello, world.’” [6] Using print statementsfor a logging system in code that is supposed tohave, otherwise, low overhead, is not an option,and so most logging systems are enabled or dis-abled at either compile time, using an#ifdef/#endif statement, or at runtime, bywrapping the logging in an if statement. An example of this idiom in the C language isshown below.

The next problem with print-based logging sys-tems is that they are both static and prone toerrors. If the programmer did not expose a pieceof information via the logging system before thecode was built, then it will not be possible, with-out modifying the code, to learn about any otherdata upon which the same function might beoperating.

Together, these two problems mean that mosthigh-performance systems, such as operating sys-tems, are shipped without logging enabled, andwhen logging is enabled, the data that is availableis limited to whatever the original programmerwished to expose.

As open-source developers, we are used to theidea that we can “just recompile the code,” but inproduction systems, that’s not always possible.Imagine you have sold a system to a large bank.At 3 a.m., the system has a fault of some sort andlogs an error. Someone in the IT department gets amessage reporting the fault, they call support, sup-port calls a programmer, the programmer thensays, “Stop the system, rebuild the code, andrerun it with logging turned on.” Handling errorsin that way is terrible both for the customer andfor the developer. The customer will be annoyed athaving to rebuild and restart their system, and thedeveloper is unlikely to get helpful informationbecause the error is now far in the past. EnterDynamic Tracing.

DTrace was designed to always be available foruse, without reducing system performance andwithout running the risk of outright crashing thesystem. The clearest way to think about DTrace isas a runtime debugger with significant loggingand statistical capabilities. DTrace avoids the over-head of printf based logging systems by using a

#ifdef LOGGINGif (log)

printf("You have written %d bytes", len);#endif /* LOGGING */

Page 16: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

14 FreeBSD Journal

few tricks to subvert the execution of compiledcode. The complete details are covered in TheDesign and Implementation of the FreeBSDOperating System [7], but, briefly, DTrace canoverride the entry or exit point of any functionthat is compiled into the kernel or into a user-space program. The way in which functions arecollected into libraries and programs is a well-defined process, and each function has a set ofinstructions that indicate where the functionbegins. When DTrace traces a function, itreplaces a few instructions with some of its ownso that when the code reaches that point,DTrace’s code gets called first, and it can collectand process the function’s argument. The practi-cal upshot of this is that when DTrace is not inuse, it has zero overhead.

Tracing for SecurityHow can we apply DTrace to the problems ofsecurity? One aspect of security is finding wherethe bad actor lies in a system. A key question toask when looking at a system is “Who did whatto whom and when?” We’ll refer to this as“Question 1.” Establishing the tree of operationssuch that we can trace it back to its root is onepart of forensic analysis that can help us find ourbad actor and also show us what we need tochange to prevent future security breaches.

Imagine that, regardless of the performancecost, we had complete transparency into everyoperation performed on a computer system.With sufficient time, analysis, and tooling, wewould be able to take the output generated bythe tracing system and find out the answer toQuestion 1.

DTrace gives us the basis on which to buildsuch a system, but there remain some challenges.In the previous section, we stated that usingsome clever tricks to replace certain instructionsat runtime, DTrace would have zero overheadwhen not in use. That feature of DTrace waswhat made it viable to ship it, by default, withSolaris, and the FreeBSD and Mac OS in the firstplace. It was not until there was a way to ship atracing system that didn’t have a performanceimpact on a running system that such a systemcould be fielded. What happens when DTracebegins tracing? Depending on what is beingtraced and how much data is being collected, theoverall performance of the system will be impact-ed to a greater or lesser degree. If the overhead

introduced by tracing gets too high, then the ker-nel will terminate the tracing as a form of self-preservation. The second tenet of the DTrace sys-tem, that the tracing system must not unduly taxthe system, is one of the key challenges of usingDTrace as a security technology. An attacker thatknows there might be tracing will first causethere to be a great deal of irrelevant load on thesystem, causing the tracing system to exit, andthen they will go about attacking the system.Another component of the CADETS project looksat the provenance of traces in order to thwartsuch attacks on the tracing system itself.

The majority of the current use cases forDTrace involve using it as a runtime debugger,turning on tracing when someone suspects thereis a problem on a system, rather than leaving thetracing running all the time. A small subset ofusers have built complex telemetry systemsaround DTrace, including Fishworks [8], but inthese first attempts at always-on tracing thenumber of things being traced was a small subsetof the possible tracepoints. To build a system thathas complete visibility, we need to not only havealways-on tracing, but to increase the perform-ance of the tracing system such that the over-head collecting the data does not overwhelm thesystem’s ability to do productive work. Anotherrequirement of a tracing system targeted at secu-rity is that trace records cannot be dropped orlost due to high load. DTrace was designed insuch a way that under high load it was accept-able to drop trace records, long before the kernelmight terminate the dtrace collection processdue to the system being unresponsive. A systemthat is trying to collect trace data for later foren-sic analysis turns that concept on its head.

Any system where tracing is always on will gen-erate a lot of data, and that data will need to beanalyzed to track down attackers and how theyare able to compromise the system. There are sev-eral systems for taking arbitrary textual outputfrom various tools and trying to make some senseof it, including Splunk [9]. The goal of the CADETSproject is to produce data for use by such tools. Itwill be much easier for consumers if the data is ina machine-readable format.

Trace Records forSoftware ToolsAs we began our work using DTrace as an

Page 17: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

May/June 2017 15

always-on tracing system, we quickly realized thatparsing the voluminous output generated by thesystem would present a problem not only forhuman analysts, but also for any tools that wouldbe consuming the data. One of the first featureswe added to DTrace for CADETS was machine-read-able output. Using the libxo library, we convertedDTrace to not only produce plain text, but alsoXML, JSON, and HTML, the three output formatssupported by libxo. At the time that we addedmachine-readable output, the Illumos version ofDTrace did have a way of outputting JSON, but thiswas via a print-like operation rather than a perva-sive change. In the CADETS version of DTrace, asingle command-line option changes all the output

the user sees from DTrace from plain text into amachine-readable format.The code samples above show the differing outputbetween the plain, textual output, and machine-readable output. Our example asks DTrace to traceall calls into the write system call. Example 1shows the default, textual output from thedtrace command, which is arranged in columns.To write a tool that parses the output requiresknowing quite a bit about the output, because thecolumns are only labeled at the start of the output.Example 2 shows the same tracepoint, but with theaddition of the -O json command line argument,instructing dtrace to give us all output in JSON

format. The machine-readable output tags eachelement, starting with the probe, which is anobject that contains several elements, including thecpu, id, func and name, all of which appeared,untagged, in the machine-unreadable output ofExample 1. One addition for machine-readable out-put is the timestamp element, which reports thetime that the probe fired, in nanoseconds, since theUNIX epoch. While a DTrace script can output thetime using either the timestamp or walltimestamp variables, we believed that having a timestamp in every machine-readable record would sim-plify building tools for security forensics. Whileprobes may fire in parallel on different cores, indicat-ed by the cpu variable, modern Intel-based systems

have a synchronizedtimestamp, whichDTrace uses, meaningthat the time stampsare an excellent indica-tion of the order ofoperations on a singlesystem.

DTrace andAuditThe audit subsys-tem has been a partof FreeBSD since2004 and is anoptional kernel com-ponent, along with auser space daemonauditd, whichimplements a “fine-grained, configurablelogging of securityrelated events [10].

It was built to meet the “Common Criteria (CC)Common Access Protection Profile (CAPP) evalua-tion,” a security standard set out by the U.S.Government [11]. The audit subsystem adds a set ofhandcrafted tracepoints via C macros to parts of thekernel where access to data and resources take place.For example, in a system with audit enabled, anytime a file descriptor is accessed, a note is made in anaudit record. Audit records are periodically flushed topermanent storage.

Our recent work with DTrace and security led us to desire a bridge between the audit systemand DTrace. Robert Watson added an auditprovider to the DTrace system in FreeBSD. A

```# dtrace -n 'syscall::write:entry'dtrace: description 'syscall::write:entry' matched 2 probesCPU ID FUNCTION:NAME

0 59780 write:entry 0 59780 write:entry

Example 1```

```# dtrace -O json -n 'syscall::write:entry'dtrace: description 'syscall::write:entry' matched 2 probesCPU ID FUNCTION:NAME{

"probe": {"timestamp": 3594774042481656,"cpu": 1,"id": 59780,"func": "write","name": "entry"

}Example 2

Page 18: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

16 FreeBSD Journal

DTrace provider gives access to a set of tracepointsfrom within DTrace. Some well-known examplesof providers are those dealing with FunctionBoundary Tracepoints (fbt), System Calls (syscall),and network protocols (tcp, udp, ip). The auditprovider gives DTrace the ability to record informa-tion about audit events that occur on the systemwhile also applying DTrace features, such as filter-ing events via predicates and collecting statisticalinformation via aggregations.

Using the audit system in the absence of DTrace,we did not have a convenient way to write runtimeanalysis scripts that allowed us to more finely targetthe processes that we wanted to investigate. Theaudit system will target a particular process, but wewanted to be able to collect data only when thatprocess took a particular action.

Consider a scenario where we want to see whois talking to a web server; we might decide thatwe only want to know about connections that arecoming from a specific set of Internet addresses,perhaps because we know those addresses havealready been identified as part of a botnet. Withthe Audit Provider we can write a simple D scriptthat asks only for the audit events relating to con-

nect(2), and then filter those events based on theIP addresses they contain. Performing this datareduction as close to the source of the informationas possible not only reduces the overall load onthe system, but it also reduces the amount of dataa human analyst or software tool has to look atduring the later analysis phase.

OpenDTrace:The Future of DTraceBesides Illumos, two other operating systems proj-ects have ported and adopted DTrace. FreeBSDhas had a port of DTrace since 2008 and Apple’sMac OS since 2007, where it is also integratedinto the Instruments performance-analysis tool.With the demise of Sun Microsystems in 2010, thedevelopment of DTrace split, with some workbeing done within Oracle, but much of it movedonto Illumos, the fully open-source follow-on toOpenSolaris. Both FreeBSD and Mac OS continuedto bring in changes from Illumos, but these were,for the most part, bug fixes rather than large newfeatures. All three groups have done what theycould to share code among themselves, but there

Premier VPS Hosting

www.rootbsd.net

RootBSD has multiple datacenter locations, and offers friendly, knowledgeable support staff.

Starting at just $20/mo you are granted access to the latestFreeBSD, full Root Access, and Private Cloud options.

Page 19: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

are significant differences among all three kernels.DTrace, although it was written in a good,portable style, requires many specialized hooksinto the operating system, and this has resulted insome level of code drift. For example, the FreeBSDversion of DTrace works on both ARMv8 andARMv7 processors, but this is not yet true onIllumos. As part of our CADETS work, we realizedthat we wanted to add new features to DTraceand also to further abstract the code from anyparticular operating system, which led us to createOpenDTrace.

The aim of OpenDTrace is to provide a single,

unified upstream for DTrace code that can thenbe easily imported into other operating systems,including FreeBSD, Mac OS, and Illumos, as wellas others. The approach is similar to that taken byOpenBSM [12] and OpenZFS [13]. With this uni-fied code base in place, we can then add featuresthat apply to all the downstream OS consumers ofDTrace far more quickly than we do today. .

AcknowledgmentThe authors thank Ripduman Sohan for com-ments that greatly improved the manuscript. •

May/June 2017 17

[1] “Jails: Confining the omnipotent root.” Poul-Henning Kamp <[email protected]> Robert N. M. Watson<[email protected]> (2000)[2] Watson, R.; Feldman, B.; Migus, A.; and Vance, C. “Design and implementation of the Trusted BSD MAC framework,”Proceedings—DARPA Information Survivability Conference and Exposition, DISCEX 2003, 1(Discex Iii), 38–49.http://doi.org/10.1109/DISCEX.2003.1194871 (2003)[3] Watson, R. N. M. and Salamon, W. “The FreeBSD Audit System,” UKUUG LISA Conference, 1–6. (2006)[4] Watson, R. N. M.; Anderson, J.; Laurie, B.; and Kennaway, K. “Capsicum: practical capabilities for UNIX,” 19th Usenix SecuritySymposium, (Figure 1), 3. http://doi.org/10.1145/2093548.2093572 (2010).[5] Cantril, B.; Shapiro, M. and Leventhal, A. “Dynamic Instrumentation of Production Systems.” (2004)[6] Davis, B. Everything you ever wanted to know about “hello, world”* (*but were afraid to ask), BSDCan. (2016)[7] McKusick, M.; Neville-Neil, G.; and Watson, R. N. M. The Design and Implementation of the FreeBSD Operating System,Second Edition. Boston, Massachusetts: Pearson Education. (2014)[8] http://dtrace.org/blogs/bmc/2008/11/10/fishworks-now-it-can-be-told/[9] https://www.splunk.com[10] https://www.freebsd.org/cgi/man.cgi?query=audit&sektion=4[11] https://www.niap-ccevs.org/pp/pp_os_ca_v1.d.pdf[12] http://www.trustedbsd.org/openbsm.html[13] http://open-zfs.org/wiki/Main_Page

JONATHAN ANDERSON is an Assistant Professor inMemorial University of Newfoundland's Department ofElectrical and Computer Engineering, where he worksat the intersection of operating systems, security, andsoftware tools such as compilers. He is a FreeBSD com-mitter and is always looking for new graduate studentswith similar interests.

GEORGE V. NEVILLE-NEIL works on networkingand operating system code for fun and profit. Healso teaches courses on various subjects related toprogramming. His areas of interest are codespelunking, operating systems, networking, and timeprotocols. He is the coauthor with Marshall KirkMcKusick and Robert N. M. Watson of The Design andImplementation of the FreeBSD Operating System. Forover 10 years he has been the columnist betterknown as Kode Vicious. He earned his bachelor’s

degree in computer science at NortheasternUniversity in Boston, Massachusetts, and is a mem-ber of ACM, the Usenix Association, and IEEE. He isan avid bicyclist and traveler and currently lives inNew York City.

ARUN THOMAS is a researcher at BAE Systems R&Dand the principal investigator of the the Causal,Adaptive, Distributed, and Efficient Tracing System(CADETS) project.

DR ROBERT N. M. WATSON is a Senior Lecturer(Associate Professor) at the University of CambridgeComputer Laboratory, where he leads research span-ning operating systems, security, and computer archi-tecture. He is a FreeBSD developer, member of theFreeBSD Foundation Board of Directors, and coauthor of The Design and Implementation of the FreeBSDOperating System (second edition).

R E F E R E N C E S

This work has been sponsored by the Defense Advanced Research Projects Agency (DARPA) and the Air Force Research Laboratory (AFRL),under contract FA8650-15-C-7558. The views, opinions, and/or findings contained in this paper are those of the authors and should not beinterpreted as representing the official views or policies, either expressed or implied, of the Department of Defense or the U.S. Government.

Page 20: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

S E ET E X TO N L Y

18 FreeBSD Journal

It is principled in that it draws from a rich history in computer security concepts such as capabili-ties, tokens that authorize their bearers to perform actions such as read from a file (using a filedescriptor as a token very like a capability) or call a method (using an object reference as a capa-bility). Capsicum is coherent in that it applies clear, simple security policies uniformly across appli-

cations. It is not possible—as can be the case in other schemes—to restrict an application’s access toone set of operations while leaving equivalent operations available for use. When we describeCapsicum as providing principled, coherent compartmentalization, we mean that it allows applica-tions to break themselves up into compartments that are isolated from each other and from otherapplications. Just as privacy-friendly companies put their users’ data encryption keys out of theirown reach, Capsicum allows applications and their compartments to give up certain abilities inorder to protect other compartments, other applications, and—ultimately—their users.

However, a significant limitation of Capsicum today is that it only works when applications vol-untarily give up the right to perform certain actions. It works with applications that understandCapsicum and that have been modified to take advantage of it; up to now, Capsicum has providedno mechanisms for confining applications without their cooperation. This is our long-term goal: toput applications into sandboxes without needing to modify the applications themselves, such that

Capsicum

Capsicum [Wat+10] is a framework for principled,coherent compartmentalization of FreeBSD applications.

TOWARD OBLIVIOUS SANDBOXING WITH

By Jonathan Anderson,Stanley Godfrey, and Robert N. M. Watson

TOWARD OBLIVIOUS SANDBOXING WITH

Capsicum [Wat+10] is a framework for principled,coherent compartmentalization of FreeBSD applications.

Page 21: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

any vulnerabilities in an application that areexploited by attackers can have their damage con-tained within an application’s memory and outputsrather than granting full access to all of a user’sdata and activities. In this article, we describerecent and ongoing work to advance this agenda,pursuing the vision of protecting ourselves fromvulnerable applications whether they like it or not.

A Taste of Capsicum Today, Capsicum allows applications to protectthemselves, other applications, and their usersfrom themselves via two mechanisms: capability mode and capabilities.

Capability Mode Capability mode is a way of confining a process tostop it from accessing any namespaces that areshared between processes such as the filesystemnamespace, the process identifier (PID) namespace,socket-address namespaces, and interprocess com-munication (IPC) namespaces (System V andPOSIX). Its only access to files and other systemobjects is mediated through capabilities, which aredescribed below. Once a process enters capabilitymode, it loses all ability to access these name-spaces and it cannot leave capability mode (norcan any processes forked from it from that pointon). This first crucial Capsicum concept creates astrong isolation: if a process cannot open anyresources and holds no capabilities, it cannot affectthe operation of other processes or leave behindany side effects. For applications to do any usefulwork, however, some communications and/or sideeffects are necessary. The key, from a security per-spective, is to ensure that these interactions occurin a controlled way.

Capabilities The second crucial concept in Capsicum—the ideaof capabilities—allows applications to be grantedaccess to potentially-shared resources in a con-trolled way. Capabilities, as described by Dennisand Van Horn in 1966 [DV66], consist of an identi-fier or address for an object together with adescription of the operations that may be per-formed on that object using the capabili-ty. In Dennis and Van Horn’s model,computation occurred within a protec-tion domain (“sphere of protection”)and accessed resources using an indexinto a supervisor-maintained C-list. Thismodel of userspace performing limited operationson system resources via indices within kernel-main-tained arrays should sound familiar to current prac-

titioners: capabilities were central to the PSOSdesign [FN79], which heavily influenced the designof Multics [SCS77], which directly inspired Unixand its file descriptors [RT78]. However, in the jour-ney from capabilities to modern file descriptors,something was lost in translation: the rigorous,principled focus on capabilities as monotonicencodings of security policy (i.e., having a set ofallowed operations that can be reduced, but neveraugmented). The Unix focus on user IDs withinfilesystems naturally led to an expansion of the roleof file descriptors, such that the set of operationspermitted via a file descriptor included operationsthat are not expressed in the descriptor itself, butare based on rights encoded in the filesystem. Forexample, most Unix-like systems will allow a userto open(2) a read-only file in a read-only modeand then fchmod(2) it to be a writable file (seeListing 1). This is an example of how file descrip-tors place more emphasis on the identity aspect ofcapabilities than on operations.

Capsicum capabilities restore to file descriptors arigorous focus on allowed operations. In FreeBSD10 and later, every file descriptor is associated witha set of explicit rights that define which operationsmay be performed on that file descriptor. Outsideof capability mode, file descriptors are opened with all rights to preserve traditional file descriptorsemantics. When descriptors are explicitly limitedor derived from other capabilities (e.g., via openat(2) relative to a directory capability), onlythose operations explicitly permitted by the capa-bility may be performed using that capability. Thereare capability rights that correspond to existingopen(2) flags such as CAP_READ andCAP_WRITE, but there are also rights that makeformerly implicit privileges a matter of explicit policy, such as CAP_SEEK, CAP_MMAP,CAP_FTRUNCATE, and CAP_FCHMOD.

Capabilities are monotonic: the holder of acapability may always give up rights associatedwith that capability with cap_rights_limit(2),but new rights can never be added to an existingcapability. If a process in capability mode requiresaccess that it does not already have, it mustacquire it from another process that rightfully hasthe authority to delegate it. Like all file descriptors,

Listing 1: File descriptors allow operations beyond thosedirectly expressed in a descriptor—in this case, a read-onlyfile descriptor is used to modify properties of the file.

int fd = open("my-data.dat", O_RDONLY);if (fchmod(fd, 0777) < 0)

err(-1, "unable_to_chmod"); // usually doesn’t run!

May/June 2017 19

Page 22: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

20 FreeBSD Journal

capabilities may be delegated from one process toanother via inheritance or IPC, but because oftheir strong monotonic guarantees, capabilitiescan be delegated with confidence: a capabilitywith CAP_READ can be shared with an untrustedprocess in capability mode with the certainknowledge that it cannot be used to fchmod(2)the file or perform any actions other thanread(2). (The experimental implementation ofcapabilities in FreeBSD 9 involved additional indirec-tion: a struct capability that contained a set ofrights and a pointer to an underlying struct file.)

Why Capsicum? Capsicum is a principled and coherent way to con-struct compartments within applications. It is prin-cipled in that it relies on a conceptually rigorousmechanism to enforce clear security policies thatcan be composed naturally due to the monotonici-ty of capabilities. As Linden observed in his 1976survey of OS security and reliability [Lin76], “a sin-gle general protection mechanism that is usedwithout exception is better than a rigid one thathas many exceptions.” Capabilities map naturallyto many program requirements, as today’s soft-ware is already structured around reference-likeaccess to files as objects with explicit methods.

Capsicum’s coherence is due to its “deep in thekernel” implementation and its simple-yet-com-plete definition of capabilities and capabilitymode. Attempts to provide “shallow” system callwrapping, exemplified by Provos’s systrace[Pro03], are unable to provide the atomicity guar-antees that are critical for security policy evalua-tion: policy enforcement is weakened when theobjects and operations seen by the kernel aresubject to races with the security policy that

authorizes the operations on those objects.Attempts to allow userspace processes to definetheir own sets of “safe” system calls, as in Linux’sseccomp-bpf [Cor12] or OpenBSD’spledge(2) mechanism [Chi15], can lead toincoherent security policies that disallow one typeof access to system resources while permitting anequivalent type of access via another path. Thiscan lead to an exposure of not-quite-sandboxedprocesses to malicious data under a false sense ofsecurity. In contrast, Capsicum’s kernel-definedcapability mode is both sufficient and necessaryto express isolation from global OS namespaces, acoherent and easy-to-understand security policy.

The simplicity and dependability of capabilitymode helps application developers use it effective-ly, so long as their applications fit the simple modelof first acquiring resources and then computing onthem. (The section called Toward ObliviousSandboxing on page 21 contains a discussion ofmore complex models.) Capsicum also requires noprivilege for an application to compartmentalizeitself, in contrast to approaches that rely onmandatory access control (MAC) such as SELinux[LS01] or AppArmor [BM06] or approaches thatrely on Linux namespaces [Bie06]. Such approachesare accessible to applications with system-adminis-trator support and/or setuid helper binaries, butCapsicum can be applied to any program compiledand run by any developer.

Compartmentalizingwith Capsicum To take advantage of Capsicum, applications(including forked children of main applicationprocesses) call cap_enter(2) before they areexposed to any untrustworthy data, e.g., networkrequests. Once a process compartmentalizes itself,it can begin performing potentially dangerousoperations such as parsing network traffic or userinput in the confidence that any maliciousexploitation will lead to, at worst, a corruption ofthe process’s explicit outputs (files, networkresponses, etc.).

This self-sandboxing approach works wellwhen a process is able to open all of theresources that it needs before entering capabilitymode. The most obvious resources to be openedbefore compartmentalization are files and sock-ets, but in a modern binary, even something assimple as cat(1) or echo(1), dynamic linkingmeans that a set of shared libraries must also beloaded before compartmentalization. As shown inFigure 1, the runtime linker runs within a process,sharing its address space and, on startup, its mainthread. Most simple applications only rely on the

Fig. 1: Applications can compartmentalizethemselves by acquiring static resources from

global namespaces before calling cap_enter(2).

Page 23: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

May/June 2017 21

runtime linker to find all their libraries on startup,after which it’s possible to enter capability modeand let the runtime linker fix up dynamic symbolsas required from already-open library files.

Other static resources that a sandboxed compart-ment might need to access include explicit files,which can be pre-opened by an application beforecalling cap_enter(2) or accessed via a pre-opened directory descriptor and then accessed bythe compartment with openat(2) and related sys-tem calls (fstatat(2), renameat(2), etc.).Implicit resources include locale files that arerequired by many libc functions, but these can alsobe pre-opened and their results cached. Moredynamic resources require a connection with theoutside world so that a sandboxed process can askan unsandboxed process to access new resources.This mode of operation is commonly employed incompartmentalized applications on many platforms,including Web browsers and—perhaps surprising-ly—all applications downloaded on MacOS throughthe Mac App Store, where all applications request-ing access to user files must go through a trustedUI called a powerbox [App16; Yee04]. To help withthese more dynamic applications’ requirements,FreeBSD includes the libcasper (CapabilityServices Provider [Zab16]) mechanism to proxyaccess to named services, some of which (e.g.,system.dns) are provided by the system itself.

With pre-opened capabilities, locale cacheing,directory descriptors, libcasper, and external prox-ies at their disposal, many applications are able tocompartmentalize themselves with Capsicum.However, this only applies to applications whoseauthors are willing to spend the effort required toadopt Capsicum features and adapt their applica-tions for compartmentalization. More impact couldbe attained if we were able to transparently sand-box applications without imposing any additionalrequirements on their authors, i.e., if we couldemploy oblivious sandboxing.

Toward ObliviousSandboxing With this goal of oblivious sandboxing in mind,work on Capsicum has been progressing acrossFreeBSD’s runtime linker, a new library, and a newcapability-aware (but not feature-complete) shell.Recently, some of these components have begunto bear fruit, leading to an exciting new develop-ment: the first transparent sandboxing of unmodi-fied applications within Capsicum’s capabilitymode. The applications that can be executedtoday in this matter are very simple, but they exe-cute without access to global namespaces andwithout any modification: rather than sandboxingthemselves, they begin life in a sandbox.

exec(2) Without a Name The traditional approach by which one applicationexecutes another is to first fork(2) a child processand then, from within that new process, to callexec(2) and start running the new program. Theexec(2) system call cleans up the memory map-pings of the current process, closes any file descrip-tors that have an O_CLOEXEC flag set (preserving allother open files, together with current environmentvariables) and transfers control to the new applica-tion. In order to do this, exec(2) must first findthe binary to be executed. Looking up a binary byname—as in the traditional exec(2) call—wouldrequire access to the global filesystem namespace;this is not permitted in capability mode. Instead,FreeBSD provides the fexecve(2) system call toexecute a binary as specified by a file descriptor(which can be a capability) rather than by pathname.On Linux, fexecve(3) is a glibc function thatuses a file descriptor to look up a symbolic link in/proc/self/fd, then calls exec(2) with thatpath name. The Linux implementation of Capsicumrequired the addition of an execveat(2) systemcall with true file descriptor semantics [Dry14].

When fexecve(2) runs, it inspects the filepassed to it to determine its type (ELF executable,script, a.out executable, etc.) and passes it to animage activator within the kernel (see Figure 2).Image activators parse various types of executablefiles and start running them; ELF image activators(32- and 64-bit) encode knowledge of runtimelinkers and how to find them in the filesystem.

Fig. 2: The FreeBSD kernel contains a number of imageactivators to parse various types of executable files andstart running them.

Page 24: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

22 FreeBSD Journal

Although various ABIs for various platforms havedefault runtime linker names, binaries can alsoexplicitly encode a path to a preferred runtimelinker, as shown in Figure 3. Whether discoveredvia a program’s internal format header or thedefault of an image activator, runtime linkers aredescribed using path names. In a conventionalexec(2) or fexecve(2) invocation, the run-time linker would be looked up using this pathand executed first, before the main function ofthe new application (illustrated in Figure 1). Insidea Capsicum compartment, however, access to

global filesystem namespaces is not permitted, soanother approach is required.

FreeBSD’s runtime ELF linker has recently beenmodified to support direct execution, i.e., onFreeBSD 12-CURRENT one can run/libexec/ld-elf.so.1 as an executable—the usage string as of writing is shown in Listing2. This ability has long been present in Linux’sld-linux.so.2, but it was not required onFreeBSD until motivated by the requirements ofoblivious sandboxing. Now, direct execution hasbeen implemented together with the ability forthe runtime linker to accept as a command-lineargument a file descriptor to link and run—thesechanges will be present in FreeBSD 12 and 11.1.Together, they allow a process in capability modethat has capabilities for a runtime linker and abinary to fexecve(2) the linker, preservingopen files including the binary’s capability, and tospecify via command-line arguments which filethe linker is to execute. The net result, shown inFigure 4, is that the specified binary is executedusing the specified runtime linker. However, with-out access to shared libraries stored in the filesys-tem, the runtime linker is not able to satisfy thedynamic code-loading requirements of the appli-cation. That requires an additional mechanism:library path descriptors.

Shared Libraries in CapabilityMode As described previously, essentially all modernexecutable files are dynamically linked and there-fore depend on access to shared libraries for theircorrect execution. In fact, the FreeBSD-derivedMacOS does not support statically linked binaries:ABI guarantees are maintained only at the inter-faces of core system libraries rather than the ker-nel [App11]. When applications compartmentalizethemselves with cap_enter(2), they can do soafter the dynamic runtime linker has discoveredlibrary dependencies and mmap(2)’ed them inplace for later linking. If an application starts run-ning before these libraries have been opened,however, the linker is unable to satisfy therequirements of dynamic symbol resolution.

Traditionally, the dynamic runtime linker hassupported a number of environment variables thatcontrol its behavior. One example,LD_LIBRARY_PATH, informs the linker of a setof directories in which additional libraries may befound. For example, a program may setLD_LIBRARY_PATH to an internal directory thatcontains application code or dynamically-loadableplugins. We have extended FreeBSD’s ELF runtimelinker to support an additional environment vari-

Fig. 3: The ELF file format includes an explicit pathto the runtime linker that is expected to be used

as an interpreter for the binary file.

Fig. 4: By directly executing the runtime linker

with a new file-descriptor argument and the LD_LIBRARY_PATH_FDS

environment variable, capsh can execute an untrusted program from within a Capsicum sandbox.

This application starts running without the ambientauthority to access global namespaces.

Page 25: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

able, LD_LIBRARY_PATH_FDS. Thisvariable allows the specification of direc-tories containing shared libraries that willbe searched in exactly the same way asLD_LIBRARY_PATH_FDS, but with onecrucial difference: instead of a colon-sep-arated list of pathnames, this variablecontains a colon-separated list of directo-ry descriptors. Since environment vari-ables and open files are both preservedacross the fexecve(2) boundary—unlike, e.g.,memory mappings—this allows a parent processto open a set of library descriptors, setLD_LIBRARY_PATH_FDS and then enter capabil-ity mode and fexecve(2) the runtime linkeritself with access to its shared library directories.Combined with the direct-execution supportdescribed in the previous section, this allows adynamically linked application to be executedfrom within a sandbox.

libpreopen: TransparentFilesystem ProxyingIt is useful to be able to execute code from withina sandbox, including code that is dynamicallylinked, but that is insufficient for the goal of obliv-ious sandboxing. Most Unix applications are writ-ten using common system calls such asaccess(2), stat(2), and open(2) to testand gain access to files within the filesystem.These system calls inherently require access to theglobal filesystem namespace, so they are not per-mitted in capability mode. It is possible to writeapplications to use fstatat(2), openat(2),etc., relative to an explicit base directory, butmany extant applications have not been writtenthis way. To achieve oblivious sandboxing, applica-tions must be confined and resources must beprovided without application modification.

We can interpose a runtime translation of system calls from capability-unsafe to capability-safe variants using the runtime linker: theLD_PRELOAD environment variable allows us toname libraries that should be loaded before anyothers. When libraries are named in this environ-ment variable without absolute paths, the runtimelinker searches through its default search paths forlibraries of the given names, but not before con-sulting LD_LIBRARY_PATH_FDS, makingLD_PRELOAD a capability-mode-compatible direc-tive. If we provide an implementation of a libcfunction such as open(2), our implementationwill take precedence over that of libc, where sys-tem calls are defined as “weak” symbols. Ourimplementation of open(2) can translate theprovided path argument into an openat(2) call,but by itself, this adaptation accomplishes noth-

ing: the application will still attempt to look up apath name that is not relative to a directory, onlythis time it will do so using the openat(2) sys-tem call instead of open(2).

The final component that is required to adaptfilesystem namespace operations is a set of pre-opened directory descriptors that other operationscan be performed relative to. This is the coreabstraction provided by libpreopen, a librarythat is—for the moment—maintained independ-ently of FreeBSD. (libpreopen can be down-loaded and built from https://github.com/musec/libpreopen). libpreopen provides a struct po_map type that is used to map direc-tory names to directory descriptors, flags, andcapability rights, as well as libc wrappers thatcan look up and query a default po_map. Forexample, when libpreopen’s implementation ofopen(2) is passed an absolute path, it looks upthe default po_map, which can be specified asdata packed into an anonymous shared memorysegment. FreeBSD’s implementation of POSIXshared memory allows a constant “path” ofSHM_ANON to be passed to shm_open(2), creat-ing a shared memory segment that can be manip-ulated by file descriptor but does not appear inthe regular POSIX shared memory namespace,making it safe for use in capability mode.libpreopen can open such a shared memorysegment, specified by file descriptor in an environ-ment variable, and unpack its data into an in-memory struct po_map object. From thatpo_map, the wrapper queries, “do you have adirectory descriptor whose name is a prefix of thisabsolute path?” If such a descriptor exists in themap, the absolute path is decomposed into adirectory descriptor and a relative path from thatdescriptor. These two elements can then bepassed to openat(2). libpreopen provides a mechanism for a

process in capability mode to access filesystemresources, as long as some directory descriptorshave been pre-opened and stored in a way that isaccessible to the library. Opening such descriptors,building a struct po_map representation,packing it into anonymous shared memory, andstoring the file descriptor of the shared memorysegment in an environment variable are all the

May/June 2017 23

Listing 2: Current usage options for FreeBSD’s ELF runtime linker.

Usage: /libexec/ld-elf.so.1 [-h] [-f <FD>] [--] <binary> [<args>] Options:

-h Display this help message-f <FD> Execute <FD> instead of searching for <binary>-- End of RTLD options<binary> Name of process to execute<args> Arguments to the executed process

Page 26: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

24 FreeBSDJournal

responsibility of the process spawning the sand-boxed child. One example of such a process iscapsh, the capability shell.

capsh: A Capability-enhancedShell The final major component of Capsicum-basedoblivious sandboxing is a program to transparentlysandbox unmodified—and unsuspecting—applica-tions. A proof-of-concept implementation of such aprogram is capsh, a shell that uses capabilities andcapability mode to sandbox applications. Hostedindependently of FreeBSD for the time being, capshallows users to execute simple unmodified applica-tions from within a Capsicum sandbox. (The capshsource code is available athttps://github.com/musec/capsh.) In its currentimplementation, the program is hardly a shell at all:it has no interactive mode, only executing a singleprogram per invocation. It also only supports simpleapplications with statically-enumerable resourcerequirements. Nonetheless, programs that fit intothis model can be executed with sandboxing frominception without program modification.capsh works by tying together the pieces of

the oblivious sandboxing puzzle described above.It finds and opens a user-specified executable file,together with a runtime linker to interpret it. Itopens library directories and stores them in the

LD_LIBRARY_PATH_FDS environment variable. Itmanipulates pre-opened directory descriptors, stor-ing them in struct po_map types provided bylibpreopen and making them available to childprocesses via shared memory and environment vari-ables. It then enters capability mode viacap_enter(2) and uses fexecve(2) to executethe runtime linker. The net result is that an unmodi-fied application starts running from within aCapsicum sandbox, as shown in Figure 4.

Oblivious Sandboxing Applications running under capsh can access onlythose resources that are explicitly delegated tothem; there can be many sources of policy as towhich resources ought to be delegated. Users run-ning capsh implicitly specify policy when theytype command-line arguments to sandboxedapplications: the presence of a filename as anargument may be an indication that the file shouldbe pre-opened before the application is executedor that permission to open the file via a proxiedmechanism such as libcasper should be grant-ed. Users may also drive policy decisions implicitlythrough interactions with a graphical user inter-face, as in the powerbox model described in theearlier section on Compartmentalizing withCapsicum, and future work on capsh may con-nect to existing models of graphical login sessionsto provide this mode of policy elicitation. Policymay also be derived from files packaged togetherwith applications: a compiler’s package metadatamay specify where its standard library is located,and capsh could pre-open that directory with aread-only capability. More sophisticated policy filescould describe limited interactions that are permit-ted with named libcasper services, leading to amore general model of a Capsicum application asshown in Figure 5. Additional exploration of mech-anism is also possible: of particular interest to theauthors of this article is the possibility of applyingLLVM-based transformation to libraries and appli-cations that embed LLVM bitcode, allowing atransparent rewriting of function calls to capabili-ty-mode–friendly APIs without needing LD_PRELOAD for interposition.

Conclusion Capsicum, a principled and coherent design forsoftware compartmentalization, has taken stridesin recent days toward a new security model.Changes in the FreeBSD ELF runtime linker,together with developments in libpreopen andcapsh, allow simple applications to be sandboxedtransparently, without any participation on thepart of the application. These foundational ele-

Fig. 5: A fully-sandboxed application can only access filesand services that have been delegated to it: the runtime link-er can find libraries in pre-opened directories usingLD_LIBRARY_PATH_FDS, libpreopen can operate on filesin pre-opened working directories—translating global-name-space–dependent system calls such as open(2) to relativevariants such as openat(2)—and libcasper can proxyaccess to namespaces of external servers.

Page 27: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

May/June 2017 25

ments have now set the stage for a deeper explo-ration of how programming models interact withthe need for compartmentalization and to whatextent software can be sandboxed obliviously,operating as normal with no requirement to knowwhether it is operating inside of a sandbox or not.A broader availability of oblivious sandboxing willallow us to move to a FreeBSD in which applica-tions “just work” and are secure by default. •

Jonathan Anderson is an Assistant Professor inMemorial University of Newfoundland's Departmentof Electrical and Computer Engineering, where heworks at the intersection of operating systems, secu-rity, and software tools such as compilers. He is aFreeBSD committer and is always looking for newgraduate students with similar interests.

Stanley Godfrey is a graduate student at MemorialUniversity of Newfoundland. His research interest isin Capsicum, FreeBSD, and operating system securi-ty. He did his undergraduate studies at HelsinkiMetropolia University of Applied Sciences, majoringin software development and graduating with aBachelor of Engineering in Information Technology.

Dr. Robert N. M. Watson is a Senior Lecturer(Associate Professor) at the University of CambridgeComputer Laboratory, where he leads researchspanning operating systems, security, and computerarchitecture. He is a FreeBSD developer, member ofthe FreeBSD Foundation Board of Directors, andcoauthor of The Design and Implementation of theFreeBSD Operating System (second edition).

[App11] Apple Inc. “Technical Q&A QA1118: Statically linked binaries on Mac OS X,” Apple Developer Guides. https://developer.apple.com/library/content/qa/qa1118. (2011)[App16] Apple Inc. “App Sandbox in Depth,” Apple Developer Guides. https://developer.apple.com/library/content/documentation/Security/Conceptual/AppSandboxDesignGuide/AppSandboxInDepth/AppSandboxInDepth.html. (2016)[Bie06] Biederman, Eric W. “Multiple Instances of the Global Linux Namespaces,” Linux Symposium Volume One, pp.101–111. https://www.landley.net/kdocs/ols/2006/ols2006v1-pages-101-112.pdf. (2006)[BM06] Bauer, Mick. “Paranoid Penguin: An Introduction to Novell AppArmor,” Linux Journal 2006.148, p. 13. ISSN: 1075-3583. URL: https://dl.acm.org/citation.cfm?id=1149839. (2006)[Chi15] Chirgwin, Richard. “Untamed pledge () aims to improve OpenBSD security: Monkey with the wrong permissions, yourprogram dies,” The Register. https://www.theregister.co.uk/2015/11/10/untamed_pledge_hopes_to_improve_openbsd_security.(2015)[Cor12] Corbet, Jonathan. “Yet another new approach to seccomp.” https://lwn.net/Articles/475043/. (2012)[Dry14] Drysdale, David. “syscalls,x86: Add execveat() system call,” Linux Kernel Mailing List.https://lkml.org/lkml/2014/5/27/147. (2014)[DV66] Dennis, Jack B. and Van Horn, Earl C. “Programming semantics for multiprogrammed computations,” Communicationsof the ACM 9.3, pp. 143–155. DOI: 10.1145/365230.365252. (1966)[FN79] Feiertag, R. J. and Neumann, Peter G. “The foundations of a provably secure operating system (PSOS),” NCC ’79:Proceedings of the 1979 AFIPS National Computer Conference. DOI: 10.1109/AFIPS.1979.116. (1979)[Lin76] Linden, Theodore. “Operating System Structures to Support Security and Reliable Software.” ACM Computing Surveys(CSUR) 8.4, pp. 409–445. DOI: 10.1145/356678.356682. (1976)[LS01] Loscocco, Peter A. and Smalley, Stephen D. “Meeting Critical Security Objectives with Security-Enhanced Linux,”Proceedings of the 2001 Ottawa Linux Symposium. https://lwn.net/2001/features/OLS/pdf/pdf/selinux.pdf. (2001)[Pro03] Provos, Niels. “Improving Host Security with System Call Policies,” Proceedings of the 12th USENIX SecuritySymposium. http://niels.xtdnet.nl/papers/systrace.pdf. (2003)[RT78] Ritchie, O. M. and Thompson, K. “The UNIX time-sharing system,” Bell System Technical Journal 57.6, pp. 1905–1929.ISSN: 0005-8580. DOI: 10.1002/j.1538-7305.1978.tb02136.x. (July 1978)[SCS77] Schroeder, Michael D.; Clark, David D.; and Saltzer, Jerome H. “The Multics kernel design project,” SOSP ’77:Proceedings of the Sixth ACM Symposium on Operating Systems Principles. ACM. DOI: 10.1145/800214.806546. (1977)[Wat+10] Watson, Robert N. M.; Anderson, Jonathan; Laurie, Ben; and Kennaway, Kris. “Capsicum: practical capabilities forUNIX,” Proceedings of the 19th USENIX Security Symposium.https://www.usenix.org/legacy/events/sec10/tech/full_papers/Watson.pdf. (2010)[Yee04] Yee, Ka-Ping. “Aligning security and usability,” IEEE Security and Privacy Magazine 2.5, pp. 48–55. ISSN: 1540-7993.DOI: 10.1109/MSP.2004.64. (2004)[Zab16] Zaborski, Mariusz. “libcasper(3),” FreeBSD Library Functions Manual. https://www.freebsd.org/cgi/man.cgi?query=libcasper. (2016)

This work has been sponsored by the Research &Development Corporation of Newfoundland &Labrador (contract 5404.1822.101), the NSERCDiscovery program (RGPIN-2015-06048), and theDefense Advanced Research Projects Agency(DARPA) and the Air Force Research Laboratory(AFRL), under contract FA8650-15-C-7558. Theviews, opinions, and/or findings contained in thispaper are those of the authors and should not beinterpreted as representing the official views orpolicies, either expressed or implied, of theDepartment of Defense or the U.S. Government.

Page 28: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

26 FreeBSD Journal

By Roberto Fernández

S E ET E X TO N L Y

SDBSGAMING

ECURITY has been discussed a lot overthe years, particularly when focusing onprotecting customers’ data. That datacan be simple data, such as an email

address or a user-name, or more elaborate datalike a password used to decrypt the shoppingdone by a customer in an online shop. Theseservices are usually based on data protection anddo not ensure that the hardware or softwarebeing used is what is actually delivered. In thecase of servers, it is useless to talk about hard-ware protection because only the system adminis-trator should have physical access to those

machines.In the gaming industry, we have software and

hardware, both of which must be checked toensure that the delivered product has not beenmanipulated. For example, manipulation could bedone by changing some of the product’s hard-ware components or by inserting a way to bypassthe protection mechanism implemented on it.

In addition to a hardware intrusion detectionsystem, it is highly recommended that the soft-ware developed for the product also have somekind of protection. FreeBSD offers several toolsfor that, starting with encrypting the disk on

IF YOU HAVE BEGUN READING THIS, IT PROBABLY MEANS YOU ARE INTERESTED IN SECURITY OR IN EMBEDDED SOFTWARE OR YOUWORK IN THE GAMING BUSINESS. IN ANY CASE, PLEASE PUT ASIDE

YOUR ANTIPSYCHOTIC MEDICATION RIGHT NOW, BECAUSE WE NEEDYOU IN YOUR MOST PARANOID STATE, READY TO WEAR THE TWO

HATS OF SECURITY, THE BLACK ONE AND THE WHITE ONE.

S

Page 29: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

May/June 2017 27

which the programs are installed and ending bydetecting whether they were somehow changed.

We should not forget that not every single pieceof code will be used in a gaming system. Forinstance, freebsd-update(8) could be used ornot. If you are not using something, it could be asecurity risk for the system if it is left on a releaseversion. If you do use it, then you have to controlwhich security issues affect your software and howpatches could be applied to the system.

But there is more to it than just protecting hard-ware or software. As a gamer, I am only concernedthat the data I saved do not get lost. When I savethe progress I made on a game, I would hate tosee that get lost and require me to redo what Ihad already done. Imagine that you have finallygotten over a scene that took maybe five hours toget through, and then the saved point was lost.Would you be “happy” or would you want to tearapart your console?

BUILDING IT AS EMBEDDEDThe best way to make sure your software has asfew security holes as possible is to use YAGNI (Youaren’t going to need it). Although this is a tech-nique for writing high-quality code, it could beused here to inform you if a program or a library isnot going to be used and thus keep you fromincluding it.

To get a better understanding of how this isaccomplished, the building section is divided intothree parts: building the world, building the ker-nel, and building ports.

Building the WorldThere are two ways to accomplish this:

• Build everything and install something.• Build only what you need.The first axiom implies a full build with well-

known commands:

And after its completion, all the items go intothe final system with a shell script. This is a way tostart if you do not want to mess around with theFreeBSD build process. An optimization techniquefor this process is to write a src.conf(5) fileand set the SRCCONF variable in your environ-ment. By doing that, your build process could

boost up a lot, depending on how many of thesettings are left out of the build process.

The second axiom requires the development ofa build variable like the ones from src.conf(5).Explaining this fully would require an entire articlein and of itself, and it is not the main focus here.With proper implementation of the variable, thebuild process could be sped up by 50% and thusavoid long waiting times.

Building the KernelInside the kernel configuration file, there are a lotof devices and options that might never get usedon your machine. Examples are NFS options orRAID devices. Again, if you do not need them inyour kernel, a new kernel’s configuration fileshould be written without them in order to get thekernel to run faster and, in case someone wants toadd hardware to the final product, to reduce thesecurity risk if it is not supported.

The kernel has already been configured to runfaster and contains support for the devices youwant, but that is not all there is to it. The bootpartition must not be encrypted (the last time Ichecked, Allan Jude was not done with the geliboot loader, at least in an automatic way to bootwithout typing the password) and theloader.conf(5) is saved there, which allows anattacker to modify it and get support for the hard-ware they want.

Building PortsThis is the trickiest question of all. We can buildthe operating system in any version of FreeBSDwithout messing around with your own build sys-tem. But this does not work with ports. If youwant to get a set of programs or libraries from theports tree and install them into your product, youneed to do something else.

There are several techniques for this. The oneused to create packages that will beused by the final user is ports-mgnt/poudriere. This toolcreates a jail and builds the ports thereusing the same version of the base sys-

tem. After building the ports and staging them,you should write a script that is able to get onlythe libraries and binaries you want. A simple wayis to write a file like the pkg-plist which is onmost of the ports and describes which compo-nents must be installed on the real system. Thenthe script mentioned before should read this file,

root@ASUS-R752L: /usr/src # make buildworldroot@ASUS-R752L: /usr/src # make installworld

DESTDIR=/work/target

Page 30: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

28 FreeBSD Journal

fetch the libraries and binaries, and copy theminto your target bintree.

Right now, you have all you need to run yourgaming system, but there are a lot of securityreports for the programs that might be installedon the product. They should be tracked down toensure that they do not affect your software andthat your product will not be compromised.

WRITING ATOMIC DATAAs explained previously, the data we write on diskmust be consistent, which means that the writingmust not be in an idle state where the data to beread will be shown as corrupted and no longerusable. There are several causes of data loss. Thefirst, and the most common in the gaming indus-try, is hard shutdown. This means that the systemis not shut down by software, but by hardwarewhich causes the data to not be entirely writtenon the disk.

FreeBSD has developed a way to ensure thatthe data is consistent by writing it entirely or notat all. The way to do this is gjournal(8).Usually, it is easier and more comfortable to writethe data needed by the user in a non-encrypteddevice or partition. That way the user can back upsaved games and a gamer profile, or copy thisinformation to bring it into another system. Also,the support team can back up user data beforestarting a reparation.

For this example, we will use a 1-TB hard diskthat contains only user data. The GEOM partitionscheme should look like the following:

In order to put some journaling into thegpt/User-Data partition, which is set in a sys-tem with 8 GB of physical memory, the followingcommands must be run:

After that, the /dev/ufs/UserData entryshould be written in the fstab(5). It is impor-tant to use that one and not the GEOM identifieror the driver name, because it can cause thewrong partition or device to be mounted.

PROTECTING VITAL DATAOne of the biggest worries in the embeddedindustry is that proprietary software gets modifiedor its behavior discovered. This is done to bypassauthentication methods implemented to deny theexecution of unknown software that allows anattacker the use of modified data or, in the caseof the gaming industry, to play an illegal copy ofyour software.

A simple way to protect your data is to encryptit with the cryptographic GEOM class, also knownas geli(8), where you select the programs to beprotected. You can encrypt the whole system oronly a partition where your programs are going tobe stored, so that in boot time, you can check ifany manipulation took place and decrypt this par-tition if none have been taken. In either case, youhave to find a secure place to store your key. Itcan be generated in runtime or stored in a chipconnected to your system, which, under certaincircumstances, will give you back the key.

The Trusted Computing Group has developed astandard for this purpose—The Trusted PlatformModule or TPM, which is a chip that has, amongother things, a non-volatile memory where you canwrite information and seal it with values stored inthe registers. Usually, the BIOS will calculate its ownchecksum and the MBR’s and store them in theTPM’s registers. After the system has completelybooted, the other registers can be written withsome calculated data to have access to the infor-mation stored in the chip’s memory which holdsthe key for decrypting the root partition.

If your platform is using a UEFI boot, then theSecure Boot is an additional step that could beused, allowing the machine to have a signed bootprocess and to look for a manipulation on theboot chain.

AUTHENTICATINGPROPRIETARY SOFTWAREThe authentication of proprietary software comesafter the machine has completely booted. Thesimplest way to do this is to write in a file whichfiles must be checked and which signature theymust have, the same thing FreeBSD does with

root@ASUS-R752L: ~ # gpart show -1 ada1

root@ASUS-R752L: ~ # gjournal label -s 16G \gpt/UserData

root@ASUS-R752L: ~ # ls /dev/ufs/UserData*UserData UserData.journalroot@ASUS-R752L: ~ # newfs -J -L UserData

/dev/gpt/UserData.journal

34 1953525101 ada1 GPT (932G)34 6 - free - (3.0K)40 1953525096 1 UserData (931G)

1953525136 5 - free - (2.5K)

Page 31: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

May/June 2017 29

release files (installation media).Another way to do it is to write a program that

reads from a binary file using a defined protocoland checks whether the programs were somehowaltered. This version requires a thorough under-standing of authentication methods and how tocalculate checksums of binary files.

But what is interesting right now is not how todetect a manipulation in your software, but whatto do when you detect one. As an example,Microsoft® bans the users who tried to use abacked-up copy of a game in a Xbox 360® fromthe online service. It prohibits the users fromupdating the system or downloading games usingthat account. Thus it leads the user to create anew one and replay all the games again in order torestore the user’s online status as gamer.

That is ideal, but if what has been altered ishow the system works, then it is a good idea to“forget” the key to decrypt the right partition, sothat the user will have a real expensive paper-weight that will never boot again. It is worthwhileto stop and consider the severity of the manipula-tion and act accordingly. For example, it is not agood idea to destroy the decryption key when theuser tries to play an illegal copy of your gameswhen the worst thing that can be achieved is play-ing the game. On the other hand, if the manipula-tion could cause the system to be used as a fraudmachine, then that option is the right call.

DETECTING HARDWAREMANIPULATIONWhen installing the product, information about the system can be obtained and stored on a filethat will be read after the system has booted tocheck whether your hardware has changed. Thelist of hardware attached to your machine andwhere it is attached can be obtained by running pciconf(8) and usbconfig(8), so you canhave control of which components are connectedvia a PCI card and which ones are attached via USBports or HUBs.

Let’s consider an example. The first of the fol-lowing scripts will get the information gathered bypciconf(8) and usbconfig(8) and store it inplain text under /var/db/hw with the namespci.db and usb.db. The second will get theinformation and compare it with the informationstored in those files. To get a better understandingof the process, let’s remove a security layer (encryp-tion, authentication, or other such mechanisms)from the file and it will be stored in plain text.

If a TPM chip is installed on your product or theUEFI Secure Boot has been selected, your BIOS hasalready been checked and therefore there is noneed to check it again. The portsysutils/dmidecode offers the option ofchecking the vendor and version of the BIOS oryour motherboard if there is a SMBIOS or DMIentry point. If dmidecode(8) does not work foryou, then you should find a way to read this infor-mation as it will be worth it in the long run.

The machine’s processor is another thing that isworth checking, and sysctl(8) is good enoughfor this. By checking the node hw.model, themodel of the CPU is returned, allowing the productto check its own processor and boot when it wasnot changed at all.

INTERFACESUp till now, we have talked about product. Thissection explains the risks in connection with any

#!/bin/sh

[ ! -d /var/db/hw ] && mkdir -p /var/db/hwpciconf -l | sort | cut -d '@' -f1 |\

while read DEVICE ; doecho ${DEVICE} | grep -q 'none' && continue

pciconf -lv ${DEVICE}done > /var/db/hw/pci.db

usbconfig | cut -d ':' -f1 | sort | \while read DEVICE ; do

usbconfig -d ${DEVICE} dump_device_descdone > /var/db/hw/usb.db

#!/bin/sh

pciconf -l | sort | cut -d '@' -f1 |\while read DEVICE ; do

echo ${DEVICE} | grep -q 'none' && continue

pciconf -lv ${DEVICE}done > /tmp/pci.db

usbconfig | cut -d ':' -f1 | sort | \while read DEVICE ; do

usbconfig -d ${DEVICE} dump_device_descdone > /tmp/usb.db

test "$(comm -3 /tmp/pci.db /var/db/hw/pci.db)"[ ${?} -eq 0 ] && exit 1test "$(comm -3 /tmp/usb.db /var/db/hw/usb.db)"[ ${?} -eq 0 ] && exit 1

exit 0

Page 32: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

30 FreeBSD Journal

interface to the external world—for instance, thenetwork card. There are several known variationsof attack attempts, especially with regard to com-munications with the external world. Let’s consid-er them through the interfaces.

NetworkToday, in the IoT (Internet of things) era, it is nor-mal to communicate a system to the Internet, andit does not matter if it is a toaster or a server. Thisis a security risk for your product, and somethought is required before connecting a device tothe external world. The question to ask is: whymust it be connected to the Internet and whichservices should be allowed?

The product can be connected to the Internetbecause there might be an online multi-player ver-sion of your game or an update service requiredby the system to keep it up-to-date with the mostsecure version of the basic software. If only thefirst condition is relevant, then the product shouldforbid the user from using other services. But ifthe product allows the user to have an HTTPbrowser, then the browser must be configured todeny pages that may compromise the product.

Another issue is the “man in the middle” prob-lem, which asks how you secure your channel toavoid the communication from being sniffed. Willit use IPsec, SSL, TLS, or are you implementing anew cryptographic layer for your communication?

I cannot really say which is better, because each

has advantages and disadvantages. It is the mis-sion of the system developer to evaluate theneeds of the final product and to set the mostsecure configuration.

USBUSBs are used for almost everything today. Thereare adapters for hard disks, Ethernet or wirelessconnections, webcams, touch screens, and serialcommunications. One might think that the USB issecure enough, but it depends on which upperlayer protocol you are using. If a program usingthe libusb(3) library or a kernel driver is devel-oped, it must be good enough to avoid memoryleaks or code injection.

OthersNo two systems are identical, so I am not in aposition to say which interfaces should be protect-ed and which not. But be paranoid, go throughthe drivers that are needed, search for securityflaws, try to hack them and improve the drivers.FreeBSD can use the help of system developersworking in the gaming industry to make it safer!

ROBERTO FERNÁNDEZ lives in Berlin, Germany,where he has worked in the gaming industry fortwo years and programmed with FreeBSD forthree years. When not spending time with his sonand wife, he plays video games.

Go to www.freebsdfoundation.org 1 yr. $19.99/Single copies $6.99 ea.

SUBSCRIBE TO DAYJ O U R N A L

AVAILABLE AT YOUR FAVORITE APP STORE NOW

TM

TM

Page 33: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

Y

GET CERTIFIED AND GET IN THERE!Go to the next level withGetting the most out of BSD operating systems requires a serious level of knowledge and expertise NEED

AN EDGE?BSD Certification can

make all the difference.Today's Internet is complex.

Companies need individuals with proven skills to work on some of the most advanced systems on the Net. With BSD Certification

YOU’LL HAVE WHAT IT TAKES!

Providing psychometrically valid, globally affordable exams in BSD Systems Administration

.... . . .

THE INTERNETNEEDS YOU

....SHOW YOUR STUFF!

Your commitment and dedication to achieving the

BSD ASSOCIATE CERTIFICATIONcan bring you to the

attention of companies that need your skills.

BSDCERTIFICATION.ORG

BSDCERTIFICATION

Page 34: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

32 FreeBSD Journal

CLOUDABI IN THEORY

CloudABI is a runtime for which you candevelop POSIX-like software. CloudABI is dif-ferent from FreeBSD's native runtime in that

it enforces programs to use dependency injection.Dependency injection is a technique that is normal-ly used in the field of object-oriented programmingto make components (classes) of a program easierto test and reuse. Instead of designing classes thatattempt to communicate with the outside worlddirectly, you design them so that communicationchannels are expressed as other objects on whichthe class may perform operations (dependencies).

A good example of dependency injection is aweb server class that depends on a separate socketobject being provided to its constructor. Such a classcan be unit tested very easily by passing in a mocksocket object that generates a fictive HTTP requestand validates the web server’s response. This class isalso reusable, as support for different network pro-tocols (IPv4 vs. IPv6, TCP vs. SCTP), cryptography(TLS), rate limiting, traffic shaping, etc., can all beadded as separate helper classes. The implementa-tion of the web server class itself can remainunchanged. A web server class that directly createsits own network socket through the socket(2)

CloudABI?W H A T S G O I N G O N W I T H

B Y E D S C H O U T E N

S E ET E X TO N L Y

The September/October 2015 issue of FreeBSD Journal featured an

article on CloudABI, an open-source project I started working on earli-

er that same year. As a fair amount of time has passed since the article

appeared, let’s take a look at some of the developments that have

taken place in the meantime and what is being planned next. But first,

a recap of what CloudABI is and what using it looks like.

Page 35: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

system call wouldn’t allow for this.The goal behind CloudABI is to introduce

dependency injection at a higher level. Instead ofapplying it to classes, we want to enforce entireprograms to have all of their dependencies injectedexplicitly on startup. In our case, dependencies arerepresented by file descriptors. Programs can nolonger open files using absolute pathnames. Filesare only accessible by injecting a file descriptor cor-responding to either the file itself or one of its par-ent directories. Programs can also no longer bindto arbitrary TCP ports. They can be injected withpre-bound sockets.

What is nice about this model is that it reducesthe need for jails, containers, and virtual machines.These technologies are often used to overcomelimitations related to the inability to run multipleconfigurations/versions of the same softwarealongside, due to a lack of isolation provided byUNIX-like systems by default. WithCloudABI, it is possible to obtain such iso-lation by simply injecting every instance ofa program with different sets of files,directories, and sockets.

It becomes very easy for an operatingsystem to sandbox processes followingthis approach. By requiring all dependen-cies to be injected, the operating systemcan simply deny access to everything else.There is no need to maintain security poli-cies separately, as is the case with frame-works like Linux’s SELinux and AppArmor.If an attacker manages to take over con-trol of a CloudABI-based process, he/shewill effectively only be able to access theresources the process needs to interactwith by design.

CloudABI has been influenced a lot byCapsicum, FreeBSD’s capability-based secu-rity framework. CloudABI differs fromCapsicum in that Capsicum still allows youto run startup code without having sand-boxing enabled. The process has to switchover to “capabilities mode” manually, usingcap_enter(2). CloudABI executables arealready sandboxed by the time the firstinstruction of the program gets executed.

The advantage of Capsicum’s model isthat it makes it possible to integrate sand-boxing into conventional UNIX programs,which are typically not designed to haveall of their dependencies injected. Theadvantage of CloudABI’s model is that it

allows us to remove all APIs that are incompatiblewith sandboxing. This reduces the effort needed toport software tremendously, as parts that need tobe modified to work with sandboxing now triggercompiler errors, as opposed to runtime errors thatmay be hard to trigger, let alone debug.

A side effect of removing all of these sandbox-ing-incompatible APIs is that it makes CloudABIso compact that it can be implemented by otheroperating systems relatively easily. This means that you can use CloudABI to build a single exe-cutable that runs on multiple platforms withoutrecompilation.

CLOUDABI IN PRACTICE

To demonstrate what it looks like to use CloudABIin practice, let’s take a look at a tiny web server forCloudABI that is capable of returning a fixed HTMLresponse back to the browser.

#include <sys/socket.h>#include <argdata.h>#include <program.h>#include <string.h>#include <unistd.h>

void program_main(const argdata_t *ad) {// Extract socket and message from config.int sockfd = -1;const char *message = "";{

argdata_map_iterator_t it;argdata_map_iterate(ad, &it);const argdata_t *key, *value;while (argdata_map_next(&it, &key, &value)) {

const char *keystr;if (argdata_get_str_c(key, &keystr) != 0)

continue;if (strcmp(keystr, "http_socket") == 0)

argdata_get_fd(value, &sockfd);else if (strcmp(keystr, "html_message") == 0)

argdata_get_str_c(value, &message);}

}

// Handle incoming requests.// TODO: Actually process HTTP requests.// TODO: Use concurrency.for (;;) {

int connfd = accept(sockfd, NULL, NULL);dprintf(connfd,

"HTTP/1.1 200 OK\r\n""Content-Type: text/html\r\n""Content-Length: %zu\r\n\r\n""%s", strlen(message), message);

close(connfd);}

}

May/June 2017 33

Page 36: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

34 FreeBSD Journal

What you may notice immediately is thatCloudABI programs get started through a functioncalled program_main(), as opposed to usingC’s standard main() function. Theprogram_main() function does away with C’sstring command-line arguments and replaces itwith a YAML/JSON-like tree structure calledArgdata. In addition to storing values likebooleans, integers, and strings, Argdata can havefile descriptors attached to it. This is the mecha-nism that is used to inject dependencies on start-up. This web server expects the Argdata to be amap (dictionary), containing both a socket foraccepting incoming requests (http_socket) anda HTML response string (html_message).

The following shell commands show how thisweb server can be built and executed. The webserver can be compiled using a cross compiler provided by the devel/cloudabi-toolchainport. Once built, it can be started with cloudabi-run, which is provided by the sysutils/cloudabi-utils port. Thecloudabi-run utility reads a YAML file fromstdin and converts it to an Argdata tree, whichis passed on to program_main(). The YAML filemay contain tags like !fd, !file, and !socket.These tags are directives for cloudabi-run toinsert file descriptors at those points in theArgdata tree. Only file descriptors referenced bythe Argdata end up in the CloudABI process.

This example shows that CloudABI can be used to build strongly sandboxed applications in anintuitive way. With the configuration passed tocloudabi-run, this web server is isolated from therest of the system completely, with the exception ofthe HTTP socket on which it may accept incomingconnections. By using Argdata, we can also omit alot of boilerplate code from our web server, like con-figuration file parsing and socket creation. All of this

functionality is implemented by cloudabi-runonce and can be reused universally.

HARDWARE ARCHITECTURES

When CloudABI was released in 2015, we onlyprovided support for creating executables for x86-64. As I believe CloudABI is a very useful tool forsandboxing software on embedded systems andappliances as well, we ported CloudABI to alsowork nicely on ARM64 around the same time theprevious article on CloudABI was published. InAugust 2016, we ported CloudABI to the 32-bitequivalents of these architectures (i686 andARMv6).

An interesting aspect of porting over CloudABIto these systems was to obtain a usable toolchain.When CloudABI was available only for x86-64, wealready used Clang as our C/C++ compiler. Clangis nice in the sense that a single installation can beused to target multiple architectures very easily. Itcan automatically infer which architecture to useby inspecting argv[0] on startup. This meantthat we only needed to extend the existingdevel/cloudabi-toolchain port to installadditional symbolic links pointing to Clang forevery architecture that we support.

At the same time, we still made use of GNUBinutils to link our executables. Binutils has thedisadvantage that an installation can only be usedto target a single hardware architecture. Even

worse, the Binutils codebase alwaysrequires a large number of modifi-cations for every pair of operatingsystem and hardware architecture itshould support.

At around the time we startedworking on supporting more archi-tectures, the LLVM project was mak-ing a lot of progress on their ownlinker, LLD. What is pretty awesomeabout LLD is that it’s essentially freeof any operating system specificcode. It’s capable of generating

binaries for many ELF-based operating systems outof the box, simply by using sane defaults thatwork well across the board. Compared to GNUBinutils, it also has a more favorable license (MITvs. GPLv3).

When we started experimenting with LLD, wenoticed there were still some blockers that pre-vented us from using it immediately. An importantstep during the linking process is that the linker

$ x86_64-unknown-cloudabi-cc -o webserver webserver.c$ cat webserver.yaml%TAG ! tag:nuxi.nl,2015:cloudabi/---http_socket: !socket

type: streambind: 0.0.0.0:8080

html_message: <marquee>Hello, world!</marquee>$ cloudabi-run webserver < webserver.yaml &$ curl http://localhost:8080/<marquee>Hello, world!</marquee>

Page 37: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

May/June 2017 35

applies relocations: a series of rules stored inobject files that describe how machine codeneeds to be adjusted to point to the correctaddresses of variables and functions when beinglinked into a program or library. We observed thatLLD applied several types of relocations incorrect-ly, causing resulting executables to access invalidmemory addresses almost instantly. This was dueto the fact that the LLD developers had mainlyfocused on getting dynamically linked executablesto work, whereas CloudABI uses static linkage.

After filing bug reports and sending variouspatches upstream, we managed to get LLD work-ing reliably for at least x86-64, i686, and ARM64.For full ARMv6 support we had to wait until LLD4.0 got released, as ARMv6 uses a custom formatfor C++ exceptions metadata (EHABI) that LLDdidn’t yet support.

LLD worked so well for us that at one point wedecided to stop using GNU Binutils entirely.Together with Google’s Fuchsia operating system,CloudABI is now one of the systems that hasswitched over to LLD completely. Thedevel/cloudabi-toolchain port nowinstalls a toolchain based on LLVM 4.0, setting upsymbolic links for *-unknown-cloudabi-ld topoint to LLD.

OPERATING SYSTEMS AND

EMULATORS

One of the original requirements for runningCloudABI programs was that you needed anoperating system kernel capable of executingthem natively. On FreeBSD, this is very easy toachieve, as FreeBSD 11 and later ship with thekernel modules for that by default (calledcloudabi32.ko and cloudabi64.ko, bothdepending on common code in cloudabi.ko).The Linux kernel patchset has also matured overtime, but hasn’t been upstreamed, meaning thatusers still need to install custom-built kernels. Onsystems like macOS, it’s undesirable to install amodified operating system kernel.

To lower the barrier for at least experimentingwith CloudABI on these systems, we’ve developedan emulator capable of running CloudABI exe-cutables on top of unmodified UNIX-like operat-ing systems. The emulator works by mapping theexecutable in the same address space and jump-ing to its entry point. Code is executed natively,without being interpreted or recompiled dynami-cally. System calls end up calling into the emula-

tor, which forwardsthem to the host oper-ating system.

While working on this,we wanted to prevent anycomplexity in the emulator thatcould easily be avoided by improv-ing CloudABI itself. For example,CloudABI executables for our 64-bit archi-tectures are now required to be position inde-pendent. Whereas systems like HardenedBSD andOpenBSD are mainly interested in using PositionIndependent Executables (PIE) to allow forAddress Space Layout Randomization (ASLR), wesee it as a useful tool for guaranteeing thatCloudABI executables can be mapped by theemulator without conflicting with address rangesused by the emulator internally.

Another improvement we’ve made is thatCloudABI executables no longer attempt toinvoke system calls through special hardwareinstructions like int 0x80 and syscall direct-ly. This is important, as we don’t want CloudABIexecutables to call into the host system’s kernelwhile emulated. They must call into the emulatorinstead. The runtime is now required to provide anin-memory shared library (a virtual Dynamic SharedObject, vDSO) to CloudABI executables on startup,exposing one function for every system call sup-ported by the runtime. When running in an emula-tor, the vDSO points to system call handlers in theemulator. When running natively, the kernel pro-vides a vDSO to the process that contains tinywrappers that do use the special hardwareinstructions to force a switch to kernel mode:

An advantage of using a vDSO this way is thatit makes it a lot easier to add and remove systemcalls over time. As system calls are now identifiedby strings, not numbers, third parties can easilyplace extensions under a custom prefix that does-n’t clash with CloudABI’s set of system calls (e.g.,acmecorp_sys_*, as opposed tocloudabi_sys_*). Programs can easily detectwhich system calls are present and absent duringstartup by simply scanning the vDSO’s symbol table.

ENTRY(cloudabi_sys_fd_sync)mov $15, %eaxsyscallret

END(cloudabi_sys_fd_sync

Page 38: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

Advertise Here

CLIMB WITH US!� LOOKINGfor qualified

job applicants?

� SELLINGproducts

or services?

Let FreeBSD Journalconnect you with a

targeted audience!

[email protected]

OR CALL

888/290-9469

Finally, we’ve also made some improvements to theway CloudABI implements Thread-Local Storage (TLS). Itis now designed in a way that the emulator can moreefficiently switch between the context used by the hostand guest process. This is achieved by requiring that theThread Control Block (TCB) of the guest always retains apointer to the TLS area of the host. When performing asystem call, the emulator can temporarily reinstate itsown TLS area by extracting it from the TCB of its guest.

One of the goals behind building an emulator usingthis approach is that having an easy way of embeddingthe execution of CloudABI programs is useful for manypurposes unrelated to emulation. One can now design asystem call tracing utility like truss(8) entirely in userspace without depending on any special kernel interfaceslike ptrace(2). Other interesting use cases include userspace deadlock detectors for multithreaded code, andfuzzers to inject random failures.

The user space emulator for CloudABI has in themeantime been integrated into cloudabi-run and caneasily be enabled by passing in the -e command lineflag. Below is a transcript of how one can build and run aCloudABI program on macOS.

BETTER C++ SUPPORT

When CloudABI was developed initially, our main focuswas to get code written in C to work. After CloudABI’s Clibrary became relatively complete and a fair number ofpackages for software written in C started to appear, weshifted our focus toward improving the experience ofporting software written in C++ to CloudABI.

$ cat hello.c #include <argdata.h>#include <program.h>#include <stdio.h>#include <stdlib.h>

void program_main(const argdata_t *ad) {int fd = -1;argdata_get_fd(ad, &fd);dprintf(fd, "Hello, world!\n");exit(0);

}

$ x86_64-unknown-cloudabi-cc -o hello hello.c$ cat hello.yaml%TAG ! tag:nuxi.nl,2015:cloudabi/---!fd stdout$ cloudabi-run hello < hello.yamlFailed to start executable: Exec format error$ cloudabi-run -e hello < hello.yamlHello, world!

TM

Page 39: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

May/June 2017 37

Early on we had managed to get LLVM’s C++runtime libraries (libcxx, libcxxabi, andlibunwind) to work, but they still required a lotof local patches. In many cases, these patcheswere cleanups not specific to CloudABI. Theymade the code more portable in general. Since theprevious article was published, we’ve been able toget almost all of these patches integrated. At thesame time, we’ve also been able to package Boost,a commonly used framework for C++.

An interesting piece of software written in C++that we’ve ported to CloudABI is LevelDB. LevelDBis a library that implements a heavily optimizedsorted key-value store, using a data structurecalled a log-structured merge-tree. It is used withinGoogle as a building block for BigTable, a databasesystem that powers many of their web services.

Porting LevelDB really demonstrated thestrength of CloudABI: by omitting any interfacesincompatible with Capsicum, it was trivial for us tofind the parts of code that needed to be patchedup to work well with sandboxing. In the case ofLevelDB, it pointed us straight to leveldb::Env,the class that implements all of the filesystem I/O.We’ve made it possible to use LevelDB in sand-boxed software by changing this class to hold afile descriptor of a directory to which filesystemoperations should be confined. Whereas youwould normally use LevelDB’s API to access a data-base as follows:

You can make use of LevelDB on CloudABI like this:

With libraries like these readily available, we’renow able to start working on bringing entire pro-grams to CloudABI. One of the lead developers ofBitcoin, Wladimir van der Laan, happened to dis-cover that most of the libraries used by Bitcoin’s

reference implementation, like Boost and LevelDB,had already been ported and packaged by us. As aresult, Wladimir has been able to successfully portbitcoind to CloudABI.

The initial goal of this project is to isolate Bitcoinfrom other processes running on the same system.A future goal is to use CloudABI to perform privi-lege separation, so that security flaws in the net-work protocol handling can’t be used by anattacker to obtain direct access to the wallet stor-ing the user’s Bitcoins.

RUNNING SANDBOXED

PYTHON CODE

In late 2015, I gave a talk about CloudABI at the32C3 security conference in Hamburg. During thistalk, I briefly mentioned that I had plans to portthe Python interpreter to CloudABI. This seemed tohave made an impression on the audience, as I gotan email from Alex Willmer not long after the con-ference, offering to help out.

During the months that followed, Alex and Iworked together a lot, coming up with patchesboth for Python and CloudABI’s C library to getPython to build as cleanly as possible. After get-ting the interpreter to build, Alex worked onextending Python’s module loader, importlib, to allow you to include paths to sys.path usingdirectory file descriptors, as opposed to using

leveldb::Options opt;opt.create_if_missing = true;opt.env = leveldb::Env::Default();leveldb::DB *db;leveldb::Status status = leveldb::DB::Open(opt, "/var/...", &db);db->Put(leveldb::WriteOptions(), "my_key", "my_value");

leveldb::Options opt;opt.create_if_missing = true;opt.env = leveldb::Env::DefaultWithDirectory(db_directory_fd);leveldb::DB *db;leveldb::Status status = leveldb::DB::Open(opt, ".", &db);db->Put(leveldb::WriteOptions(), "my_key", "my_value");

Page 40: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

38 FreeBSD Journal

pathname strings. Finally, I worked on integratingthe Python interpreter with Argdata, so that itcan be launched through cloudabi-run.

Below is a demonstration of what it looks liketo run a simple “Hello, world” script using ourcopy of Python. During its lifetime, the interpreteronly has access to Python’s modules directory, thescript we’re trying to execute, and the terminal towhich the script should write its message .

Though it may at first seem somewhat com-plex to run Python this way, the requirement forinjecting all dependencies of Python explicitlydoes have the advantage that it’s now a lot easierto maintain multiple Python environments. Eachof these environments may have different ver-sions of third-party modules installed. Thisapproach effectively means there is no longer anyneed to use Python’s own virtualenv to

achieve isolation between envi-ronments. This can already beaccomplished by injectingPython with the right set of filedescriptors.

The example given above is,of course, only intended toscratch the surface of how youcan use CloudABI’s copy ofPython. On the CloudABI devel-opment blog, you can find anarticle that explains how youcan use socketserver andhttp.server to build yourown sandboxed web services. Inthe meantime, we’re also work-ing on porting the Django webapplication framework. A pre-liminary version that is capableof serving requests has alreadybeen packaged.

$ cat hello.pyimport ioimport sys

stream = io.TextIOWrapper(sys.argdata['terminal'],encoding='UTF-8')

print(sys.argdata['message'], file=stream)$ cat hello.yaml%TAG ! tag:nuxi.nl,2015:cloudabi/---path:- !file

path: /usr/local/x86_64-unknown-cloudabi/lib/python3.6script: !file

path: hello.pyargs:

terminal: !fd stdoutmessage: Hello, world!

$ cloudabi-run \/usr/local/x86_64-unknown-cloudabi/bin/python3 < hello.yaml

Hello, world!

By Brooks Davis, Robert Norton, Jonathan Woodruff & Robert N. M. Watson

Choose ebook, print or combo. You’ll learn:• Use boot environment, make the riskiest sysadmin tasks boring.• Delegate filesystem privileges to users.• Containerize ZFS datasets with jails.• Quickly and efficiently replicate data between machines• Split layers off of mirrors.• Optimize ZFS block storage.• Handle large storage arrays.• Select caching strategies to improve performance.• Manage next-generation storage hardware.• Identify and remove bottlenecks.• Build screaming fast database storage.• Dive deep into pools, metaslabs, and more!

ZFS experts make their serversNow you can too. Get a copy of.....

WHETHER YOU MANAGE A SINGLE SMALL SERVER OR INTERNATIONAL DATACENTERS, SIMPLIFY YOUR STORAGE WITH FREEBSD MASTERY: ADVANCED ZFS. GET IT TODAY!

Link to:

Page 41: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

FORMALIZATION OF THE BINARY

INTERFACE

All of CloudABI’s low-level data types and constantswere originally defined through a set of C headerfiles. The problem with these header files was thatthey became pretty complex over time. As we allowyou to run 32-bit CloudABI executables both on 32-bit and 64-bit systems, we had to maintaincopies of the definitions that either assumed the tar-get’s native pointer size (used by user space) or thatassumed 32-bit or 64-bit pointers explicitly (used bykernel space). CloudABI’s system call table wastranslated to FreeBSD’s in-kernel format and kept insync by hand.

To clean this up, Maurice Bos has worked on a project to formalize CloudABI. All CloudABI datatypes, constants, and system calls are nowdescribed in a programming language independ-ent notation in a file called cloudabi.txt. Belowis an excerpt of what the definitions related to thecloudabi_sys_fd_read() system call look like:

From this file, we now automatically generate Cheader files, system call tables, vDSOs, andHTML/Markdown documentation. We eventuallywant to use this framework to generate low-levelbindings for other languages as well (e.g., Rust, Go),so that they can be ported to CloudABI withoutdepending on any definitions that are copied byhand.

ARGDATA:

NOW A SEPARATE LIBRARY

Though Argdata was originally designed just to beused for passing startup configuration to CloudABIprograms, it’s actually a pretty flexible and efficientbinary serialization library under the hood.Compared to MessagePack, a similar encoding for-mat, it allows for efficient random access of datawithout performing full deserialization. Comparedto libnv(3), a serialization library in FreeBSD’sbase system, it has a more conventional data modeland a simpler API.

Earlier this year, Maurice Bos expressed interestin using Argdata as a format for serializing RPCmessages in a non-CloudABI application written inC++. As the Argdata library was tightly integratedinto CloudABI’s C library, Maurice spent some timemaking it more portable and turning it into a sepa-rate library.

At the same time, he also wrote some reallygood C++ bindings for Argdata, making use of var-

ious features provided bymodern revisions of the lan-guage (C++11, C++14,C++17). Maps and sequencescan be iterated using C++’srange-based for loops. Bymaking use ofstd::optional<T>, C++’simplementation of a ‘”maybetype,” it becomes easier todeal with potential type mis-matches. Strings are returnedas std::string_viewobjects, meaning they can beused by C++ code withoutcopying them out of the seri-alized data or allocatingcopies on the heap.

opaque uint32 fd| A file descriptor number.

struct iovec| A region of memory for scatter/gather reads.range void buf

| The address and length of the buffer to be filled.

syscall fd_read| Reads from a file descriptor.in

fd fd| The file descriptor from which data should be| read.

crange iovec iovs| List of scatter/gather vectors where data| should be stored.

outsize nread

| The number of bytes read.

May/June 2017 39

Page 42: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

40 FreeBSD Journal

Below is an example of what the C++ bindingslook like when used in practice. When comparedto the web server provided in the introductionthat uses the C API, the C++ code is a lot morecompact and easier to understand.

NEXT PROJECT:

CLOUDABI FOR KUBERNETES?

Over the last couple years there has been a lot ofdevelopment in the area of cluster managementsystems. These systems allow you to treat a largegroup of servers as a single pool of computingresources on which you can schedule jobs. Onesystem that is gaining popularity is Kubernetes,designed by Google and funded through theLinux Foundation’s recently founded Cloud NativeComputing Foundation (CNCF). Kubernetes canrun any software that has been packaged as acontainer (using Docker, rkt, etc.).

What I’ve observed while using Kubernetes isthat it has a number of quirks stemming from thefact that it has to be able to run software that isnot dependency injected. For example, becauseprograms running in containers are free to bind toarbitrary network ports, every job (“pod”) in the

cluster must have a unique IPv4 address. Thismakes Kubernetes consume a lot of networkaddresses and makes routing tables of nodes in thecluster very complex. Conversely, as most conven-tional software can only connect to a single net-

work address to reachbackend services,Kubernetes does lots ofTCP-level load balancingfor internal traffic, whichmakes tracing and debug-ging very complicated.

The fact that jobs inthe cluster are able to cre-ate arbitrary network con-nections also means thatsecurity between podscan only be achieved if allcontainers running in thecluster are configured tomake use of cryptogra-phy, authentication, andauthorization (e.g., usingSSL with a per-service cus-tom trust chain).Unfortunately, peoplehardly ever bother settingthat up correctly, meaningit is generally not safe tooperate a singleKubernetes cluster for amultitenant environment.

An interesting devel-opment for us is that as of version 1.5,Kubernetes no longer communicates with the sys-tem’s container engine directly. When Kuberneteswants to start a container on a node in the clus-ter, it sends an RPC for that to an additionalprocess, called the Container Runtime Interface(CRI). This mechanism is intended to allow peopleto experiment with custom container formatsmore easily by developing their own CRIs.

In our case, we could implement our own CRIthat allows us to run CloudABI processes directlyon top of Kubernetes. By using CloudABI, wecan enforce jobs running on the cluster to haveall of their network connectivity injected by ahelper process. This helper process can ensure alltraffic in the cluster is encrypted, authorized, andload balanced. Software running on the clusterwill no longer need to care about the model ofthe underlying network.

#include <argdata.hpp>#include <cstdlib>#include <optional>#include <program.h>

void program_main(const argdata_t *ad) {// Scan through all configuration options and // extract values.std::optional<int> database_directory, logfile, http_socket;for (auto [key, value] : ad->as_map()) {

if (auto keystr = key->get_str(); keystr) {if (*keystr == "database_directory")

database_directory = value->get_fd();else if (*keystr == "logfile")

logfile = value->get_fd();else if (*keystr == "http_socket")

http_socket = value->get_fd();}

}

// Terminate if we didn’t get started with all // necessary descriptors.if (!database_directory || !logfile || !http_socket)

std::exit(1);

…}

Page 43: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

WRAPPING UP

I hope this article has shown that a lot of interest-ing things have happened with CloudABI over thelast year and that there are even more excitingthings planned. As CloudABI is part of FreeBSD 11and most new features have already been mergedinto 11-STABLE, CloudABI has become an easy-to-use tool for creating secure and testable software.If you maintain a piece of software that could ben-efit from this, be sure to experiment with buildingit for CloudABI.

As most of the discussion around CloudABItakes place on IRC, feel free to join #cloudabi onEFnet if you have any questions or simply want tostay informed about what’s going on.

LINKS

CloudABI on FreeBSD:https://nuxi.nl/cloudabi/freebsd/

CloudABI development blog: https://nuxi.nl/blog/

CloudABI on GitHub: https://github.com/NuxiNL

BitCoin for CloudABI: https://laanwj.github.io/

May/June 2017 41

ED SCHOUTEN has been a developer at the FreeBSD Project since 2008. His contributions includeFreeBSD 8's SMP-safe TTY layer, the initial import of Clang into FreeBSD 9, and the initial version of thevt(4) console driver that eventually made its way into FreeBSD 10.

CloudABI has been developed by the author’s company, Nuxi, based in the Netherlands. It will always be available as open-source software, free of charge. Nuxi offers commercial support, consulting, andtraining for CloudABI. If you are interested in using CloudABI in any of your products, be sure to get intouch with Nuxi at [email protected].

BUILD SOMETHING GREATER.

Dell is now part of the Dell Technologies family. So you can make your mark in everything from storage and big data to the Internet of Things. Apply at Dell.com/careers

Page 44: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

S E ET E X TO N L Y

Tell us a bit about yourself, your back-ground, and your interests.

• I was born in and live in Novokuznetsk(founded in 1618), West Siberia, RussianFederation. I lived in Novosibirsk for severalyears while studying at Novosibirsk StateUniversity (NSU). I have been interested inmathematics since secondary school when Igot my first programmable calculator in thelate ’80s. That was the Russian model MK-61with 15 addressable memory registers and 4operating registers combined as a stack forreverse Polish notation operators(https://upload.wikimedia.org/wikipedia/ru/2/27/Elektronika-Mk-61.JPG ). It had memoryfor 105 program instructions. My first pro-grams were created using its machine code.

Later I took a computer class that had Z80A-based MSX-2 computers and then an IBM PS/2pilot class (80286-based Model 30). Writing

simple video games was as much fun for me asplaying them. I understood that computerswould be my future fun and livelihood.

I've enjoyed reading lots of books. My fatherwas a literature teacher and our home librarywas large. Also, I studied music and played gui-tar in a school band. I must admit that com-puter studies banished playing music from mylife, but my taste for good music persisted.

How did you first learn about FreeBSDand what about FreeBSD interested you?

• I started to use FreeBSD 2.2.5-RELEASE in1996 during my early university days, as abeginner C and Java programmer. FreeBSDwas already used on some computers in theInstitute of Computational Mathematics andMathematical Geophysics, also known as theComputing Center of the Russian Academy ofSciences, Siberian Branch, where I was work-ing and doing my diploma work. Those werethe good old times when FreeBSD had/etc/sysconfig instead of /etc/rc.conf and onecould use FreeBSD with 4 megabytes of RAM,but sysinstall needed 5 megabytes or early-activated swap.

Initially, I was only familiar with Microsoftoperating systems: MSX-DOS, MS-DOS, andearly versions of Windows. FreeBSD opened awhole new universe for me—the open-sourceapproach and the Unix world as well as theability to change systems at once, networktransparent graphics, and lots of other things.

I continued to use FreeBSD after I graduatedfrom NSU with a degree in mathematics in

new facesof FreeBSD BY DRU LAVIGNE

This column aims to shine a spotlight on contributorswho recently received their commit bit and to intro-duce them to the FreeBSD community. This month, thespotlight is on Eugene Grosbein, a longtime contribu-tor who transitioned to a ports committer in March.

For those interested in becominga FreeBSD committer: you can do it!Help us improve FreeBSD code, docs,or ports. Improve yourself while youhave fun doing this, talk to other con-tributors and committers, offer helpand ask for help from them, andyou’ll make it. We need new peoplehere!.” —EUGENE GROSBEIN

42 FreeBSD Journal

Page 45: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

1999, when I installed 3.2-RELEASE on my homedesktop and started to use FreeBSD at work in anISP environment (2.2.8-RELEASE). That was whenI started to contribute to the FreeBSD Project byfiling Problem Reports using GNATS (PR 15301was my first one).

How did you end up becoming a committer?

• In 2001, I created my first port, databases/libudbc (retired now), for Openlink UniversalDataBase Connectivity SDK, which I used to con-nect a FreeBSD-based web server to a DEC RDBdatabase running under VAX/OpenVMS. Sincethen I have been on the FreeBSD “AdditionalContributors” list. In the meantime, I continuedto employ FreeBSD in several ISP and other com-mercial environments. I even ran 64-bit FreeBSD4.x/alpha for a time. Since then, I’ve filed about340 PRs, submitting patches when I could. I thinkthe number of my unclosed reports just reachedcritical mass so Vsevolod Stakhov and Andrej

Zverev offered me a promotion to ports commitbit and became my mentors. We talked about iton an IRC channel and I gratefully agreed.

How has your experience been since joiningthe FreeBSD Project? Do you have any advicefor readers who may be interested in alsobecoming a FreeBSD committer?

• In spite of my history with the FreeBSD Projectas a user and contributor, being a committerproved to be a different matter. There are a lot ofthings to be learned from the Committers Guidein regard to respecting other people’s work andkeeping the repository contents maintainable andconsistent. But, it's lots of fun to see how youcan make it better. •

DRU LAVIGNE is a doc committer for theFreeBSD Project and Chair of the BSDCertification Group.

May/June 2017 43

ver time, Unix operating systems have evolved a variety ofmechanisms to launch programs and influence their execution.To put this into perspective, let’s take a quick tour of the dif-

ferent kinds of programs that run on a typical FreeBSD system.

When the computer first boots up, therc(8) mechanism launches programs called“services” that can perform one-time systeminitialization tasks, or execute daemons torun in the background. These services canalso be stopped and started while the com-puter is running.

Every minute the computer is running, thecron(8) daemon wakes up and launchesscheduled tasks. Cron also supports the abil-ity to run at(1) jobs after a certain timeinterval has passed. It can also runbatch(1) jobs when the system load aver-

age falls below a certain threshold.There are many other mechanisms for

launching programs. Inetd(8) launchesprograms when an incoming socket is creat-ed. nice(1) runs programs with a modi-fied priority level. chroot(8) launches pro-grams with an alternate root directory.service(8) launches programs with a“supervisor” process that can restart theprogram if it crashes. Jail managers likejail(8) launch programs within a jailedenvironment. Virtual machine managers likeiohyve(8) launch programs that execute a

OO

The Browser-based Edition(DE) is now available for

viewing as a PDF withprintable option.

TM

The Broswer-based DE format offers sub-scribers the same features as the App, butpermits viewing the Journal through yourfavorite browser. Unlike the Apps, you canview the DE as PDF pages and print them.

To order a subscription, or get back issues, and other

Foundation interests, go to

www.freebsdfoundation.org

JOURNAL

NEWViewing &

Printable

Options

$19.99$6.99

YEAR SUB

SINGLE COPY

The DE, like the App, is anindividual product. You willget an email notification eachtime an issue is released.

Page 46: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

44 FreeBSD Journal

S E ET E X TO N L Y

It’s hard to believe that it was 10 years agowhen I stumbled across Designing BSDRootkits, by Joseph Kong, as I was wanderingaround a local bookstore. Not only was this a

book on a technical topic that I happen to findpretty fascinating, but it also used FreeBSD as thereference platform! Since the focus of this issue issecurity, I thought it would be fun to dust off mycopy and take another look.

At this point you might be asking what exactlyis a rootkit? Once an attacker gains unauthorizedaccess to your system, the goal is usually to avoiddetection and maintain control of the machine foras long as possible. This is usually accomplishedby deploying a rootkit, which is a collection ofutilities designed to hide the intrusion as well asto maintain privileged access. While this bookfocuses on the FreeBSD operating system, theconcepts that are covered can be applied to otheroperating systems as well. Even if you do notwork with FreeBSD on a day-to-day basis, you willstill gain valuable insights into computer securitythat can be applied to pretty much any system.

One of the things I love about this book is thatit cuts right to the chase. This first chapter imme-diately dives into the topic of loadable kernelmodules, which is the double-edge sword thatallows your system administrator to add function-ality to your running system without having toreboot, but is used by an attacker to load mali-

cious code that can help cover their tracks. Kongwastes no time, because on the fourth page ofthis book you are presented with a bare boneskernel module which you can build, load, andunload on your machine. Following a tutorial styleformat, each chapter slowly builds upon the previ-ous one, introducing more advance functionality.By the end of the book your simple kernel modulewhich just printed ‘Hello, world!’ will be a com-plete and functional rootkit capable of evadinghost-based intrusion detection systems such astripwire.

In the final chapter, the focus shifts fromoffense to defense with a chapter dedicated tothe detection of rootkits. Amusingly, this is alsothe second shortest chapter with very few piecesof example code. The overall theme is that once arootkit is in place, detection isn’t easy. Softwaredesigned to detect a rootkit can just as easily besubverted by the very rootkit it is looking for, andthe kernel is a very big place that offers lots ofplaces to hide. However, since you are now armedwith a much better understanding of what isactually going on under the hood, you will be in asuperior position should you ever have to do bat-tle with a rootkit.

While the book is thin, weighing in at only 142pages, it manages to pack in a wealth of informa-tion and example code. To get the most out ofthis book, it will be helpful to have some under-

Publisher.......................................................No Starch Press (2007)

Print List Price.........................................................................$29.95

Digital List Price......................................................................$23.95

ISBN-10 ..........................................................................1593271425

ISBN-13...................................................................978-1593271428

Pages ................................................................................142 pages

by Steven KreuzerBOOKreviewDesigning BSD Rootkits:An Introduction to Kernel Hackingby Joseph Kong

Page 47: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

standing of both C and assembly as well as someknowledge of FreeBSD kernel internals. However, Ifound Kong's tutorial-style approach combined withthe very well documented code examples made theconcepts incredibly accessible. The fact that eachchapter provided at least one real-world applicationfor each topic that he covers makes it even easier toexperiment and build upon the examples on yourown. You could read the book from cover to coverin a few hours, but I would highly recommend thatyou sit yourself down in front of your computer witha copy of this by your side while you play aroundwith the provided examples. It becomes quite easyto quickly lose a few hours as you load and unload akernel module designed for nefarious purposes.

Don’t let the fact that the book was published adecade ago scare you away. Not only does this bookserve as a great introduction to FreeBSD kernel mod-ule programming, the methods and tactics that are

covered are still in use today. One thing to note isthat a lot has changed in FreeBSD, and when thisbook was first written, the author was usingFreeBSD 6.0-STABLE on a 32-bit platform. Not all thecode examples may properly compile on a newersystem, so it becomes an exercise for the reader todebug and update the provided examples. With allthat said, I highly recommend you add a copy toyour bookshelf. •

STEVEN KREUZER is a FreeBSD Developerand Unix Systems Administrator with aninterest in retro-computing and air-cooledVolkswagens. He lives in Queens, NewYork, with his wife, two daughters, anddog.

May/June 2017 45

Thank you! The FreesBSD Foundation would like to acknowledge the following companies for their continued support of the Project. Because of generous donations such as these we are able to continue moving the Project forward.

Uranium

Iridium

Silver

Please check out the full list of generous community investors at freebsdfoundation.org/donate/sponsors

Are you a fan of FreeBSD? Help us give back to the Project and donate today! freebsdfoundation.org/donate/

TM

TM

Page 48: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

svnUPDATE•

S E ET E X TO N L Y

by Steven Kreuzer

Add ipfw_pmod kernel module (https://svnweb.freebsd.org/changeset/base/316435)

This new module is designed for modification ofpackets from any protocol, only implementing

TCP MSS modification at this time. It adds the exter-nal action handler for "tcp-setmss" action. A rulewith tcp-setmss action performs an additional checkfor protocol and TCP flags. If SYN flag is present, itparses TCP options and modifies the MSS option if itsvalue is greater than the configured value in the rule.

Capsicumize pom (https://svnweb.freebsd.org/changeset/base/317165)

Ihad a good laugh when I saw this commitbecause I was not aware that out-of-the-box

FreeBSD provided the ability to report the phases ofthe moon. While this appears to be a fairly trivialapplication with little attack surface, it has beenconverted to run in a capsicum sandbox so you canrest easy at night.

Set the arm64 Execute-never bits in moreplaces (https://svnweb.freebsd.org/changeset/base/316761)

Each memory region on arm64 can be tagged asnot containing executable code. If the Execute-

never, XN, bit of the descriptor is set to 1, anyattempt to execute an instruction in that regionresults in a permission fault. Set the Execute-neverbits when mapping device memory, as the hardwaremay perform speculative instruction fetches. Set thePrivileged Execute-never bit on userspace memory tostop the kernel if it is tricked into executing it.

Enable the process state bit to disableaccess to userspace from the kernel onARMv8.1 (https://svnweb.freebsd.org/changeset/base/316756)

ARMv8.1 introduced a new Privileged Access-never (PAN) state bit. This bit provides control

that prevents privileged access to user data unlessexplicitly enabled, which provides additional securityagainst possible software attacks.

Like everything else in life, security is a trade-off, and it usually comes at the expense of theend user losing functionality or having toendure a decrease in performance or through-put. What this usually means is that unless youhave people and/or an organization that havedecided to take security seriously, some of thefirst things which get turned off are the fire-walls or the access control security policies. Tomake matters even worse, sometimes the secu-rity will cause your application to break in anonobvious way, and taking the sledgehammerapproach of turning everything off until itworks again is all too common. At the other endof the spectrum, you can be left with a falsesense of security and have machines that areactually vulnerable because of a very subtletypo you made in a complex security subsys-tem with difficult-to-understand syntax.

Security is hard, and it’s even harder to get itright. One of the great things about theFreeBSD Project is that system security has

always been a major focus. Developers spend a lotof time and effort to provide a secure and reliablesystem so that someone with little to no experi-ence using FreeBSD can build a new machine andconnect it directly to the Internet and be reason-ably confident that nothing bad will happen. Overthe past few years, FreeBSD has been used as aproving ground for some new and innovative con-cepts in computer security that are both light-weight and mostly transparent, resulting in a muchbetter experience for both system administratorsand end users alike. Since the topic of this issue issecurity, I wanted to use this installment of svnupdate to highlight some of the recent improve-ments designed to keep you and your data safe,be it improvements in encryption, extendingFreeBSD to support hardware security features thatare being baked into the CPU itself, or just theongoing work to explicitly define what an applica-tion can and cannot do with Capsicum.

46 FreeBSD Journal

Page 49: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

May/June 2017 47

3BSD-licensed implementation of theChaCha20 stream cipher, intended or usedby the upcoming arc4random replacement(https://svnweb.freebsd.org/changeset/base/316982)

The ChaCha20 cipher is a high-speed cipher pub-lished by Daniel J. Bernstein in 2008. It is consid-

erably faster than AES in software-only implementa-tions due to its use of CPU-friendly Addition-rotation-XOR operations.

Replace the RC4 algorithm for generatingin-kernel secure random numbers withChaCha20 (https://svnweb.freebsd.org/changeset/base/317015)

Writing software is hard but we are pretty fortunatethat contributions being made to FreeBSD are

coming from a pool of very talented people. However,these developers are human, and humans sometimesmake mistakes. But we have a very active communityof people who continually audit the code base lookingfor potential security vulnerabilities, both large andsmall. While not as exciting as some of the otherchanges I mentioned, I thought it would be interestingto take a look at a few small snippets of code thatappear innocuous, but could have potentially beenused as an avenue to compromise your system.

Use strlcpy to appease static checkers indummynet (https://svnweb.freebsd.org/changeset/base/316777)

Prevent some heap overflows in restore(8)(https://svnweb.freebsd.org/changeset/base/316799)

Prevent possible sscanf() overflow inmixer(8) (https://svnweb.freebsd.org/changeset/base/317596)

Fix some trivial argv buffer overruns inctm(1) (https://svnweb.freebsd.org/changeset/base/316795)

Avoid possible overflow via environmentvariable in loader(8) (https://svnweb.freebsd.org/changeset/base/316771)

Fix an out-of-bounds write when a zero-length buffer is passed (https://svnweb.freebsd.org/changeset/base/316768)

STEVEN KREUZER is a FreeBSD Developerand Unix Systems Administrator with aninterest in retro-computing and air-cooledVolkswagens. He lives in Queens, New York,with his wife, two daughters, and dog.

T

Write For Us!Write

For Us!

Contact Jim Maurer ([email protected] )with your article ideas.

JOURNALTMTM

TM

Page 50: May/June 2017 Journal - 04:03.… · By Michael W Lucas FreeBSD’s Firewall Feast 4 As CloudABI is part of FreeBSD 11 and most new features have already been merged into 11-STABLE,

Linuxhotel Hackathon and DevSummit • July 7–9 • Essen, Germany https://wiki.freebsd.org/DevSummit/201707 • The 2nd annual

FreeBSD Hackathon and DevSummit will be held in the park-like setting of the Linuxhotel Villa Vogelsang. Registration is required and there is a nominal fee to cover lodging and food.

48 FreeBSD Journal

The following BSD-related conferences will takeplace during the summer of 2017.

BSDCam • August 2–4 • Cambridge, UKhttps://wiki.freebsd.org/DevSummit/201708 • The Annual Cambridge

FreeBSD DevSummit will take place at the University of Cambridge. Registration isrequired, lodging is available, and there is a nominal fee to cover meals.

SUMMER OF 2017 BY DRU LAVIGNE

Openhelp • August 12–16 • Cincinnati, OHhttps://conf.openhelp.cc/ • This annual conference brings together leaders inopen-source documentation and support, as well as people from across the technicalcommunications industry who are interested in community-based help. Several mem-bers of the FreeBSD documentation team will be in attendance and there will be a

FreeBSD documentation sprint on August 14 and 15 (in-person and on #bsddocs on IRC EFNET). There is a nominal registration fee to attend this event.

Order Back Issues @ www.freebsdfoundation.org/journal

JOURNALTM

FreeBSD support for the ARM architecture has historically been a grassroots affair, and

for a whole host of embedded platforms this works well. With the launch of ARMv8,

the development of ARM-based systems has expanded into the enterprise space, both

from a server infrastructure and a network infrastructure standpoint. To support the

enterprise market, it was clear we needed a more aggressive development model to

deliver the most complete and competitive offering of FreeBSD.

ARM is short on in-house expertise with FreeBSD. We have many who havean interest, but no real developmental expertise. With the advent of version 8of the ARM architecture (which enables 64-bit support), we looked at ways of

bli F BSD th hit t Si did ’t t t it l

AARMMv8ENABLED AAS TTIER 11 AARCHITECTURE:

A CCOLLABORATIVE DDEVELOPMENT PPROJECT

y

Following is a taxonomy of filesystem and storage development from 1979 tothe present, with the BSD Fast Filesystem as its focus. It describes the early per-formance work done by increasing the disk blocksize and by being aware of the disk geometry and using that knowledge to optimize rotational layout.With the abstraction of the geometry in the late 1980s and the ability of thehardware to cache and handle multiple requests, filesystems performanceceased trying to track geometry and instead sought to maximize performanceby doing contiguous file layout. Small file performance was optimized throughthe use of techniques such as journaling and soft updates. By the late 1990s,filesystems had to be redesigned to handle the ever-growing disk capacities.The addition of snapshots allowed for faster and more frequent backups. Multi-processing support got added to utilize all the CPUs found in the increasinglyubiquitous multi-core processors. The increasingly harsh environment of theInternet required greater data protection provided by access-control lists andmandatory-access controls Ultimately the addition of metadata optimization

by Marshall Kirk McKusick