Secure Coding in C and C 2nd Edition Robert C. Seacord - Read the ebook now or download it for a full experience
Secure Coding in C and C 2nd Edition Robert C. Seacord - Read the ebook now or download it for a full experience
com
https://ebookname.com/product/secure-coding-in-c-and-c-2nd-
edition-robert-c-seacord/
OR CLICK HERE
DOWLOAD EBOOK
https://ebookname.com/product/c-gotchas-avoiding-common-problems-
in-coding-and-design-1st-edition-stephen-c-dewhurst/
https://ebookname.com/product/embedded-c-coding-standard-michael-
barr/
https://ebookname.com/product/existentialism-2nd-edition-robert-
c-solomon/
https://ebookname.com/product/autocad-2015-and-autocad-
lt-2015-essentials-autodesk-official-press-1st-edition-scott-
onstott/
A Better Way To Sell Mastery Of Sales Through Mastery
Of Self 1st Edition De Silva
https://ebookname.com/product/a-better-way-to-sell-mastery-of-
sales-through-mastery-of-self-1st-edition-de-silva/
https://ebookname.com/product/brewer-s-dictionary-of-phrase-
fable-properyly-bookmarked-17th-edition-john-ayto/
https://ebookname.com/product/chemical-and-biochemical-
engineering-new-materials-and-developed-components-1st-edition-
ali-pourhashemi/
https://ebookname.com/product/case-histories-in-international-
politics-7th-edition-edition-kendall-w-stiles/
https://ebookname.com/product/transgenic-plants-methods-and-
protocols-1st-edition-luis-herrera-estrella/
Body in Postwar Japanese Fiction Asia s Transformations
Dougl Slaymaker
https://ebookname.com/product/body-in-postwar-japanese-fiction-
asia-s-transformations-dougl-slaymaker/
From the Library of Sarah Renko
Secure Coding
in C and C++
Second Edition
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.
Robert C. Seacord
Foreword xvii
Preface xxi
Acknowledgments xxv
About the Author xxvii
vii
1.5 Summary 27
1.6 Further Reading 28
Chapter 2 Strings 29
2.1 Character Strings 29
String Data Type 30
UTF-8 32
Wide Strings 33
String Literals 34
Strings in C++ 36
Character Types 37
Sizing Strings 39
2.2 Common String Manipulation Errors 42
Improperly Bounded String Copies 42
Off-by-One Errors 47
Null-Termination Errors 48
String Truncation 49
String Errors without Functions 49
2.3 String Vulnerabilities and Exploits 50
Tainted Data 51
Security Flaw: IsPasswordOK 52
Buffer Overflows 53
Process Memory Organization 54
Stack Management 55
Stack Smashing 59
Code Injection 64
Arc Injection 69
Return-Oriented Programming 71
2.4 Mitigation Strategies for Strings 72
String Handling 73
C11 Annex K Bounds-Checking Interfaces 73
Dynamic Allocation Functions 76
C++ std::basic_string 80
Invalidating String Object References 81
Other Common Mistakes in basic_string Usage 83
2.5 String-Handling Functions 84
gets() 84
C99 84
C11 Annex K Bounds-Checking Interfaces: gets_s() 86
Dynamic Allocation Functions 87
strcpy() and strcat() 89
C99 89
strncpy() and strncat() 93
memcpy() and memmove() 100
strlen() 100
Wraparound 229
Signed Integer Types 231
Signed Integer Ranges 235
Integer Overflow 237
Character Types 240
Data Models 241
Other Integer Types 241
5.3 Integer Conversions 246
Converting Integers 246
Integer Conversion Rank 246
Integer Promotions 247
Usual Arithmetic Conversions 249
Conversions from Unsigned Integer Types 250
Conversions from Signed Integer Types 253
Conversion Implications 256
5.4 Integer Operations 256
Assignment 258
Addition 260
Subtraction 267
Multiplication 269
Division and Remainder 274
Shifts 279
5.5 Integer Vulnerabilities 283
Vulnerabilities 283
Wraparound 283
Conversion and Truncation Errors 285
Nonexceptional Integer Logic Errors 287
5.6 Mitigation Strategies 288
Integer Type Selection 289
Abstract Data Types 291
Arbitrary-Precision Arithmetic 292
Range Checking 293
Precondition and Postcondition Testing 295
Secure Integer Libraries 297
Overflow Detection 299
Compiler-Generated Runtime Checks 300
Verifiably In-Range Operations 301
As-If Infinitely Ranged Integer Model 303
Testing and Analysis 304
5.7 Summary 307
GCC 318
Visual C++ 318
6.3 Exploiting Formatted Output Functions 319
Buffer Overflow 320
Output Streams 321
Crashing a Program 321
Viewing Stack Content 322
Viewing Memory Content 324
Overwriting Memory 326
Internationalization 331
Wide-Character Format String Vulnerabilities 332
6.4 Stack Randomization 332
Defeating Stack Randomization 332
Writing Addresses in Two Words 334
Direct Argument Access 335
6.5 Mitigation Strategies 337
Exclude User Input from Format Strings 338
Dynamic Use of Static Content 338
Restricting Bytes Written 339
C11 Annex K Bounds-Checking Interfaces 340
iostream versus stdio 341
Testing 342
Compiler Checks 342
Static Taint Analysis 343
Modifying the Variadic Function Implementation 344
Exec Shield 346
FormatGuard 346
Static Binary Analysis 347
6.6 Notable Vulnerabilities 348
Washington University FTP Daemon 348
CDE ToolTalk 348
Ettercap Version NG-0.7.2 349
6.7 Summary 349
6.8 Further Reading 351
The sufferings of the sea may appear easy to bear in the prospect of its pearls;
But I have erred, for its waves are not worth one hundred munns of gold.”[44]
And in the Turkish poem of Khusrev and Shīrīn, by Shayki, ob. A.D.
1426 (Mr Gibb’s Ottoman Poems, p. 6):
No power was left him, neither sport nor pleasure,
He bit his finger, wildered beyond measure.
Page 27. “Driven forth from the village”; the text adds; “and they
deprived him of whatever they had given.”
Page 27. “For the sake of God:” a common phrase among Muslims.
A rather humorous example of its use occurs in the Gulistān (chap,
iv, tale 14): A harsh-voiced man was reading the Kur’ān in a loud
tone. A pious man passed by him, and said: “What is thy monthly
stipend?”—“Nothing,” he replied.—“Why then,” he inquired, “dost
thou give thyself all this trouble?”—“I read for the sake of God,” he
replied.—“For God sake, then, don’t read,” said he.
Page 27. “A pearl of such exquisite beauty,” &c.—In the East it is
popularly believed that the pearl is formed in the oyster from a rain-
drop: Sa’dī, in the fourth book of his Bustān, has some beautiful
verses on this notion, in which he inculcates the practice of humility.
Pearls are called marvārīd, “production of light,” and, usually when
they are unpierced, lū’lū’, “luminous,” “brilliant.” They are divided
into twelve classes, each having a distinctive name, according to their
“water” or lustre; the first class being called shahvār, “the regal,” the
clearest, purest, and most lustrous. Pearls are also divided into
twelve classes, according to shape. They are further divided, in
respect of size, into fifteen classes, according to the number of holes
in the different sieves through which they are passed, from the
smallest, of which twelve hundred weigh a miskal, up to the largest,
of which forty weigh a miskal. The best pearl-fisheries are at Ceylon,
and in the Persian Gulf, at Bahrayn, Kīsh, and Sharak; but the
Arabian pearls are less prized than the Indian. Their colour and
quality are said to depend on the bottom of the sea where they are
produced: in black slime they are dark; in shallow waters, yellowish.
—Tavernier mentions a remarkable pearl found at Katifa, in Arabia,
the fishery probably alluded to by Pliny (Nat. Hist. b. ix, c. 54), which
he purchased for £10,000 of our money! It is said to be now in the
possession of the Shah of Persia.
Page 28. “He put three of the pearls into his mouth and the other
three among his clothes.”—It is customary for travellers and others
in the East to conceal their money and valuables about their clothes
and in the folds of their turbans. Many Oriental stories illustrate this
practice. For example, in the tale of the Poor Ropemaker (Arabian
Nights—vol. vi, of Jonathan Scott’s edition), he receives a sum of
money from a benevolent stranger, and having laid out a moiety of it
in material for his trade, he places the remainder within the folds of
his turban-cloth, but unluckily a bird snatches it off his head and
flies away with it. And in the Talmud there is a story of a poor
Hebrew, named Joseph, who paid great respect to the Sabbath. This
man had a wealthy neighbour, who was a firm believer in judicial
astrology, and having been told by a sagacious professor of the
science that all his riches should one day become the property of the
Sabbath-observing Joseph, he straightway sold his estate and
invested the proceeds in a large diamond, which he secretly sewed
within his turban, and departed in a vessel for some distant country
—thus preventing, as he fondly imagined, the verification of the
astrologer’s prediction. But his precautions were of no avail, for
while standing on the deck of the vessel, a sudden gust of wind
carried his turban, with all his wealth, into the sea. What became of
the ruined man after this misfortune we are not informed. But we are
told that, some time after this accident, the pious Joseph went to the
market and bought a fish to furnish his table on the Sabbath eve. On
opening the fish, the diamond which his old neighbour had lost with
his turban was found in its stomach—and thus was the good man’s
strict observance of the Sabbath rewarded, and the astrologer’s
prediction fulfilled to the letter.
Page 28. The unlucky Merchant’s adventure with the covetous and
dishonest jeweller finds a curious parallel in an incident in the “Story
of the Jackal, the Barber, and the Brāhman,” one of the charming
fairy tales in Miss Frere’s Old Deccan Days. The poor Brāhman,
however, though robbed of the precious stones he offers to the
jeweller for sale, escapes home all safe, unlike the Merchant of our
story. Possibly the incident in both tales had a common origin;—yet
the “roguery of villanous man” (to employ honest Jack Falstaff’s
phrase) is pretty much alike in all ages and countries!
Page 29. “They distributed some money among those who were
confined.”—Alms are recommended in many passages of the Kur’ān:
“Pay your legal alms,” ii, 43; “alms are to be distributed to the poor
and the needy ... for the redemption of captives, insolvent debtors,
and, for religion’s sake, unto the traveller,” ix, 53, 60. Alms are of two
kinds: (1) obligatory (or zakāt), ii, 172; and (2) voluntary (or
sadakāt), as in the present instance. In scripture we find a trace of
the same doctrine: see Daniel iv, 27. The Khalif `Omar Ibn `Abdu-’l-
`Azīz used to say: “Prayer carries us half-way to God; Fasting brings
us to the door of the palace; and Alms procure us admission.” And
assuredly no Eastern moralist has more frequently or more
impressively and beautifully inculcated the duty of alms-giving and
of liberality than Sa`dī. He tells us in the Gulistān, ii, 49, that on the
monument of Bahrām Gūr, a famous Persian King, was written: “The
liberal hand is better than the strong arm;” and adds: “Distribute in
alms the tithe of thy wealth; for the more the husbandman loppeth
off the exuberance of the vine, the more it will yield of grapes.” And
in his Bustān, or Fruit-Garden, b. ii, he says: “Bestow thy gold and
thy wealth while they are thine; for when thou art gone they will be
no longer in thy power.... Distribute thy treasure readily to-day, for
to-morrow the key may no longer be in thy hand.... Exert thyself to
cast a covering over the poor, that God’s own veil may be a covering
for thee.”[45]
Page 30. “When he had related the story of the Merchant and of
the pearls which they had given him”—the text adds, “and the other
five divers had confirmed what he said.”
Page 30. “He was then led away to execution; and the King caused
to be proclaimed throughout the city,” &c. So, too, in the Thousand
and One Nights, the Barber relates how his Fourth Brother was
punished with a hundred lashes, “after which they mounted him
upon a camel, and proclaimed before him: ‘This is the recompense of
him who breaketh into men’s houses.’” Morier, in his Second
Journey, gives a graphic description of the punishment of
Muhammad Zamān Khān, governor of Astrābād, who, in 1814,
“entered into a league with the Turkmāns, disavowed the King’s
authority, and even made pretensions to the royal power and
prerogative.” The King offered a reward for his capture; and the
people of Astrābād surrounded the traitor’s palace, forced their way
into the room where he was seated, seized and bound him, and
carried him before the King. “When he had reached the camp, the
King ordered the chief of his camel-artillery to put a mock-crown
upon the rebel’s head, armlets on his arms, a sword by his side; to
mount him upon an ass, with his face towards the tail and the tail in
his hand; then to parade him throughout the camp, and to proclaim:
‘This is he who wished to be King!’ After this was over, and the
people had mocked and insulted him, he was brought before the
King, who called for the looties and ordered them to turn him into
ridicule by making him dance and perform antics against his will. He
then ordered that whoever chose might spit in his face. After this he
received the bastinado on the soles of his feet, which was
administered by the chiefs of his own tribe; and some time after he
had his eyes put out.—The strong coincidence,” adds Morier,
“between these details and the most awfully affecting part of our own
scripture history is a striking illustration of the permanence of
Eastern manners.”
Page 30. “Appointed him keeper of the treasury.”—The sudden
elevation of persons from a humble and even distressed condition to
places of great dignity and wealth has ever been a characteristic of
the absolute monarchs of Eastern countries, as well as the
degradation and ruin, frequently from mere caprice, and seldom with
any justification, of men of the highest rank. The most remarkable
instance of the many which Oriental history presents is the execrable
conduct of the Khalif Hārūnu-’r-Rāshīd, so undeservedly celebrated
in the Thousand and One Nights, in murdering his principal Vizier
Ja`far and utterly ruining the other members of the noble house of
Barmak (the Barmecides of our common translation of the Arabian
Nights), all of whom were as famed for their unbounded liberality as
for their brilliant abilities. An interesting account of the Barmakis
and their ruin is given in Dr Jonathan Scott’s Tales, Anecdotes, &c.,
from the Arabic and Persian.
Page 32. “Put out the Merchant’s eyes.”—A too common and
barbarous punishment in the East. In Turkey a needle was used for
this purpose in the case of state prisoners. The Arabian poet-hero
`Antar is said to have blinded his implacable and treacherous enemy
Wezār by passing a red-hot sword-blade close before his eyes. Years
afterwards the blinded chief executed poetical justice by slaying
`Antar with a poisoned arrow, which he shot at him on the bank of
the Euphrates.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookname.com