⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 security

📁 linux下qmail的源码 本人加了一些注释
💻
字号:
Background: Every few months CERT announces Yet Another Security Hole InSendmail---something that lets local or even remote users take completecontrol of the machine. I'm sure there are many more holes waiting to bediscovered; sendmail's design means that any minor bug in 46000 lines ofcode is a major security risk. Other popular mailers, such as Smail, andeven mailing-list managers, such as Majordomo, seem nearly as bad.Note added in 1998: I wrote the above paragraph in December 1995, whenthe latest version of sendmail was 8.6.12 (with 41000 lines of code).Fourteen security holes were discovered from sendmail 8.6.12 through8.8.5. See http://pobox.com/~djb/docs/maildisasters/sendmail.html.I started working on qmail because I was sick of this cycle of doom.Here are some of the things I did to make sure that qmail will never letan intruder into your machine.1. Programs and files are not addresses. Don't treat them as addresses.sendmail treats programs and files as addresses. Obviously random peoplecan't be allowed to execute arbitrary programs or write to arbitraryfiles, so sendmail goes through horrendous contortions trying to keeptrack of whether a local user was ``responsible'' for an address. Thishas proven to be an unmitigated disaster.In qmail, programs and files are not addresses. The local deliveryagent, qmail-local, can run programs or write to files as directed by~user/.qmail, but it's always running as that user. (The notion of``user'' is configurable, but root is never a user. To prevent sillymistakes, qmail-local makes sure that neither ~user nor ~user/.qmail isgroup-writable or world-writable.)Security impact: .qmail, like .cshrc and .exrc and various other files,means that anyone who can write arbitrary files as a user can executearbitrary programs as that user. That's it.2. Do as little as possible in setuid programs.A setuid program must operate in a very dangerous environment: a user isunder complete control of its fds, args, environ, cwd, tty, rlimits,timers, signals, and more. Even worse, the list of controlled itemsvaries from one vendor's UNIX to the next, so it is very difficult towrite portable code that cleans up everything.Of the twenty most recent sendmail security holes, eleven worked onlybecause the entire sendmail system is setuid.Only one qmail program is setuid: qmail-queue. Its only purpose is toadd a new mail message to the outgoing queue.3. Do as little as possible as root.The entire sendmail system runs as root, so there's no way that itsmistakes can be caught by the operating system's built-in protections.In contrast, only two qmail programs, qmail-start and qmail-lspawn,run as root.4. Move separate functions into mutually untrusting programs.Five of the qmail programs---qmail-smtpd, qmail-send, qmail-rspawn,qmail-remote, and tcp-env---are not security-critical. Even if all ofthese programs are completely compromised, so that an intruder hascontrol over the qmaild, qmails, and qmailr accounts and the mail queue,he still can't take over your system. None of the other programs trustthe results from these five.In fact, these programs don't even trust each other. They are in threegroups: tcp-env and qmail-smtpd, which run as qmaild; qmail-rspawn andqmail-remote, which run as qmailr; and qmail-send, the queue manager,which runs as qmails. Each group is immune from attacks by the others.(From root's point of view, as long as root doesn't send any mail, onlyqmail-start and qmail-lspawn are security-critical. They don't write anyfiles or start any other programs as root.)5. Don't parse.I have discovered that there are two types of command interfaces in theworld of computing: good interfaces and user interfaces.The essence of user interfaces is _parsing_---converting an unstructuredsequence of commands, in a format usually determined more by psychologythan by solid engineering, into structured data.When another programmer wants to talk to a user interface, he has to_quote_: convert his structured data into an unstructured sequence ofcommands that the parser will, he hopes, convert back into the originalstructured data.This situation is a recipe for disaster. The parser often has bugs: itfails to handle some inputs according to the documented interface. Thequoter often has bugs: it produces outputs that do not have the rightmeaning. Only on rare joyous occasions does it happen that the parserand the quoter both misinterpret the interface in the same way.When the original data is controlled by a malicious user, many of thesebugs translate into security holes. Some examples: the Linux login-froot security hole; the classic find | xargs rm security hole; theMajordomo injection security hole. Even a simple parser like getopt iscomplicated enough for people to screw up the quoting.In qmail, all the internal file structures are incredibly simple: text0lines beginning with single-character commands. (text0 format means thatlines are separated by a 0 byte instead of line feed.) The program-levelinterfaces don't take options.All the complexity of parsing RFC 822 address lists and rewritingheaders is in the qmail-inject program, which runs without privilegesand is essentially part of the UA.6. Keep it simple, stupid.See BLURB for some of the reasons that qmail is so much smaller thansendmail. There's nothing inherently complicated about writing a mailer.(Except RFC 822 support; but that's only in qmail-inject.) Securityholes can't show up in features that don't exist. 7. Write bug-free code.I've mostly given up on the standard C library. Many of its facilities,particularly stdio, seem designed to encourage bugs. A big chunk ofqmail is stolen from a basic C library that I've been developing forseveral years for a variety of applications. The stralloc concept andgetln() make it very easy to avoid buffer overruns, memory leaks, andartificial line length limits.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -