Secure Coding
in C and C++
Second Edition
The SEI Series in Software Engineering
Software Engineering Institute of Carnegie Mellon University and Addison-Wesley
Visit informit.com/sei for a complete list of available publications.
The SEI Series in Software Engineering is a collaborative undertaking of the
Carnegie Mellon Software Engineering Institute (SEI) and Addison-Wesley to develop
and publish books on software engineering and related topics. The common goal of the
SEI and Addison-Wesley is to provide the most current information on these topics in a
form that is easily usable by practitioners and students.
Titles in the series describe frameworks, tools, methods, and technologies designed to
help organizations, teams, and individuals improve their technical or management capa-
bilities. Some books describe processes and practices for developing higher-quality soft-
ware, acquiring programs for complex systems, or delivering services more effectively.
Other books focus on software and system architecture and product-line development.
Still others, from the SEI’s CERT Program, describe technologies and practices needed
to manage software and network security risk. These and all titles in the series address
critical problems in software engineering for which practical solutions are available.
Make sure to connect with us!
informit.com/socialconnect
Secure Coding
in C and C++
Second Edition
Robert C. Seacord
Upper Saddle River, NJ Boston Indianapolis San Francisco
New York Toronto Montreal London Munich Paris Madrid
Capetown Sydney Tokyo Singapore Mexico City
The SEI Series in Software Engineering
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trade-
marks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the
designations have been printed with initial capital letters or in all capitals.
CMM, CMMI, Capability Maturity Model, Capability Maturity Modeling, Carnegie Mellon, CERT, and CERT
Coordination Center are registered in the U.S. Patent and Trademark Office by Carnegie Mellon University.
ATAM; Architecture Tradeoff Analysis Method; CMM Integration; COTS Usage-Risk Evaluation; CURE; EPIC;
Evolutionary Process for Integrating COTS Based Systems; Framework for Software Product Line Practice; IDEAL;
Interim Profile; OAR; OCTAVE; Operationally Critical Threat, Asset, and Vulnerability Evaluation; Options Anal-
ysis for Reengineering; Personal Software Process; PLTP; Product Line Technical Probe; PSP; SCAMPI; SCAMPI
Lead Appraiser; SCAMPI Lead Assessor; SCE; SEI; SEPG; Team Software Process; and TSP are service marks of
Carnegie Mellon University.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trade-
marks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the
designations have been printed with initial capital letters or in all capitals.
The author and publisher have taken care in the preparation of this book, but make no expressed or implied
warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental
or consequential damages in connection with or arising out of the use of the information or programs contained
herein.
The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special
sales, which may include electronic versions and/or custom covers and content particular to your business, train-
ing goals, marketing focus, and branding interests. For more information, please contact:
U.S. Corporate and Government Sales
(800) 382-3419
corpsales@pearsontechgroup.com
For sales outside the United States, please contact:
International Sales
international@pearsoned.com
Visit us on the Web: informit.com/aw
Library of Congress Cataloging Control Number: 2013932290
Copyright © 2013 Pearson Education, Inc.
All rights reserved. Printed in the United States of America. This publication is protected by copyright, and per-
mission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. To
obtain permission to use material from this work, please submit a written request to Pearson Education, Inc.,
Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or you may fax your request to
(201) 236-3290.
ISBN-13: 978-0-321-82213-0
ISBN-10:
0-321-82213-7
Text printed in the United States on recycled paper at Edwards Brothers Malloy in Ann Arbor, Michigan.
First printing, March 2013
To my wife, Rhonda, and our children, Chelsea and Jordan
This page intentionally left blank
Contents
Foreword
Preface
Acknowledgments
About the Author
Chapter 1 Running with Scissors
1.1 Gauging the Threat
1.2
What Is the Cost?
Who Is the Threat?
Software Security
Security Concepts
Security Policy
Security Flaws
Vulnerabilities
Exploits
Mitigations
1.3 C and C++
A Brief History
What Is the Problem with C?
Legacy Code
Other Languages
1.4 Development Platforms
Operating Systems
Compilers
xvii
xxi
xxv
xxvii
1
5
6
8
11
12
14
14
15
16
17
17
19
21
24
25
25
26
26
vii