0906.2512
0906.2512
net/publication/45856644
CITATIONS READS
0 160
3 authors, including:
Serguei Mokhov
Concordia University Montreal
192 PUBLICATIONS 1,045 CITATIONS
SEE PROFILE
Some of the authors of this publication are also working on these related projects:
All content following this page was uploaded by Serguei Mokhov on 29 February 2016.
Represented by:
Marc-André Laverdière-Papineau
Serguei A. Mokhov
Djamel Benredjem
{ma laver,mokhov,d benred}@ciise.concordia.ca
April 2006
Contents
1 Introduction 1
1.1 Security Problems in C Standard Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Introducing ISO/IEC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 ISO/IEC TR 24731 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2 Architecture 3
2.1 Principles and Philosophy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2 Summary of Siemens Four View Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2.1 Conceptual View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2.2 Module View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2.3 Execution View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2.4 Code View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2.5 Conceptual View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2.5.1 Conceptual Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2.5.2 Configurations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2.5.3 Protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2.5.4 Resource Budgeting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2.6 Module View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2.6.1 Layering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2.6.2 Interface Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3 Execution View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3.1 Runtime Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3.2 Communication Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3.3 Execution Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.4 Code View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
i
ii On Implementation of a Safer C Library, ISO/IEC TR 24731.
3 Implementation 12
3.1 Run-time Constraint Handling API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.2 Constraint Violation Information Encapsulation API . . . . . . . . . . . . . . . . . . . . . . 12
3.3 Constraint Enumeration and Validator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.4 Constraint Handling Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4 Results 17
4.1 Implemented API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.1.1 Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.1.1.1 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.1.1.2 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.1.2 Private Constraint Handling API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.1.2.1 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.1.2.2 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4.2 Constraint Handling In Action – stdio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4.3 Constraint Handling In Action – string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
5 Conclusions 25
5.1 Summary of the Difficulties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
5.2 Limitations So Far . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
5.3 Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
5.4 Future Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Bibliography 27
Chapter 1
Introduction
The functions standardized as part of ISO C 1999 and their addendums improved very little the security
options from the previously available library.
The largest flaw remained that no function asked for the buffer size of destination buffers for any func-
tion copying data into a user-supplied buffer. According to earlier research we performed [PMB], we know
that error condition handling was the first solution to security vulnerabilities, followed by precondition
validation. The standard C functions typically perform little precondition validation and error handling,
allowing for a wide range of security issues to be introduced in their use.
For example:
char *strncat(char *dest, const char *src, size t n); does not null-terminate, can still
overflow
char *strtok(char * restrict s1, const char * restrict s2); not reentrant
size t strlen(const char *s); can iterate in the memory up to
an invalid page and cause a pro-
gram crash
This effort remained not enough, and many projects developed additional functions, namely:
The International Standardization Organization (ISO) and the International Electrotechnical Commission
(IEC) are standard-making bodies headquartered in Geneva (Switzerland) [Sec05].
1
2 On Implementation of a Safer C Library, ISO/IEC TR 24731.
Both organizations are constituted from an international membership, with local member organizations
involved in standard-making activities as well. For example, we have the Standards Council of Canada,
American National Standards Institute, Deutsches Institut für Normung, and Association française de
normalisation [ISO06b].
ISO and IEC collaborate closely on standards related to computer equipment and information tech-
nologies.
These organizations established a hierarchical structure under JTC 1 - Joint Technical Committee
on Information Technology. JTC 1 is subdivided in 17 subcommittees, one of which (SC 22) deals with
programming languages, with a working group for each programming languages [ISO06a].
The C language is normalized by ISO/IEC JTC 1/ SC 22/ WG 14. Its members include representatives
from Microsoft, SEI/CMU, Cisco, Intel, etc [SC2]. The Computer Security Laboratory of CIISE, through
Pr. Debbabi, is a member of this Working Group as Canadian representative with voting rights.
In the ISO jargon, TR 24731 [WG106] is a Technical Report Type 2 [ISO05, ISO], which means that the
document is not a standard, but a direction for future normalization. This specification is currently in the
draft state.
Titled “TR 24731: Safer C library functions”, it defines 41 new library functions for memory copying,
string handling (both for normal and wide character strings), time printing, sorting, searching etc. Another
inovation it brings is a constraint handling architecture, forcing error handling when certain security-related
preconditions are violated when the functions are called. It also specifies the null-termination of all strings
manipulated through its function and introduces a new unsigned integer type that helps preventing integer
overflows and underflows. It is currently implemented by Microsoft as part of their Visual Studio 2005
[Sea05].
Chapter 2
Architecture
In this chapter, we examine the architecture of our implementation of ISO/IEC TR 24731. We first
introduce our architectural philosophy before informing the reader about the Siemens Four View Model,
an architectural methodology for the conception of large-scale software systems.
Afterwards, we examine each of the view, as architected for our library.
Finally, we conclude with other software engineering matters that were of high importance in the
development of our implementation.
The library specification imposes that the functions be in addition of other standard functions, in the same
header files. However, we do not want our implementation to re-implement the standard C library, nor
do we want to augment an existing implementation and be bound to a specific platform. The compromise
solution is to organize the code to be using the low-level implementation of any existing C library, such as
the one from GNU [gp], FreeBSD or OpenBSD.
In short, our principles are:
Standards Compliance We will implement the library using features available only in ISO C99 and
POSIX.
C Library Independence We will architect in a way that prevents us being tied to the underlying C
library.
Reasonable Efficiency We will architect and implement an efficient library, but will avoid advanced
programming tricks that improve the efficiency at the cost of maintainability and readability.
3
4 On Implementation of a Safer C Library, ISO/IEC TR 24731.
Simplicity And Maintainability We will target a simplistic and easy to maintain organization of the
source.
Architectural Consistency We will consistently implement our architectural approach. We could say
that we will have a “template” approach.
Separation of Concerns We will isolate separate concerns between modules and within modules to
encourage reuse and code simplicity.
Functional Grouping We will have functional coupling between within a module, meaning that func-
tions will have a commonality of type, such as I/O, string manipulations, etc.
We decided to use the Siemens Four View Model for our architectural description, mostly due to previous
experience using the technology. A detailed description with case studies can be found in [HNS00]. This
methodology is introduced by scientists of Siemens Corporate Research and has been successfully applied to
a variety of systems, many of which with real-time and embedded requirements. Please refer to Figure 2.1
for an abstract presentation of the view. In the context of this project report, we inform the reader of the
basic principles of each view.
The conceptual view defines the conceptual components and their conceptual connectors, as well as their
conceptual configuration. The architect, in this phase, must also specify resource budgets.
In the module view, the architect maps the conceptually-defined elements into modules and layers. The
architect must then define the interface to the modules.
In the execution view, the architect must define the runtime entities, the communication paths and the
execution configuration. It essentially means the mapping of modules to threads and processes, and to
define the inter-process communication mechanisms to be used. In the case of our library, there are no
threads of execution per se, as such, this architectural step was skipped.
In the code view, the architect must define the source components, intermediate components and deploy-
ment components, followed by the build procedure and configuration management.
On Implementation of a Safer C Library, ISO/IEC TR 24731. 5
We divided our architecture in a few modules, isolating the core functionalities from each library and
grouping functions per library. We also decided to use wrappers to a C library implementation. This
overview can be seen in Figure Figure 2.2.
2.2.5.2 Configurations
The only options in configuration relate to the wrappers to use for a specific C library. Since this is
decided at compilation time and that only one is possible in any case, we decided not to further specify
configurations.
2.2.5.3 Protocols
Because of the simplistic nature of the components (direction function calls) and that all components need
to be re-entrant, we conclude that no protocols are necessary.
6 On Implementation of a Safer C Library, ISO/IEC TR 24731.
We did not specify any resource budget for any components due to the lack of specific constraints.
2.2.6.1 Layering
We divided our library between layers when we found significant redundancy for some operations. We
decided to keep the memory copying for wide characters apart from the one without wide characters due
to the risk of integer overflows that could result in faulty logic, and thus deserving a centralization of the
functionality.
The complete view of our layering is included in Figure Figure 2.3.
The only interface that needed to be designed was related to the constraint validation. Please refer to the
specification given in section Section 3.1
In the case of our library, there are no threads of execution per se, as the thread is provided by the calling
program(s).
It was resolved that the functions would all communicate through message passing.
Each of stdio.h, stdlib.h, string.h, time.h, and wchar.h are mapped into a corresponding directory,
as a module. The folder named test copies the previous structure, and contains test programs exclusively.
The include directory contains the .h files to be included by external programs linking to the library.
8 On Implementation of a Safer C Library, ISO/IEC TR 24731.
The folder named adapters holds a sub-directory per C library implementation to adapt to, and each of
those adapters mirror the base directory structure. Each interface function was implemented in a file with
its name, to facilitate maintainability. Related functions are grouped in the file to which it is the most
logically related.
We established a naming convention for functions as follows:
In order to facilitate the building process, we decided that each directory of functionality and adapters
will assemble all intermediary (.o) files into an archive (.a).
The only deployment component will be a .so file that includes our implementation and the underlying
library.
The build process is organized as a hierarchical organization of makefiles. Each makefile cross-reference
the makefiles for its dependencies. The intermediary objects defined are the standard .o that are also
grouped in an archive (.a). The testers are built separately from the library itself for efficiency and faster
compilation.
The make files are designed to adapt to both Linux and Windows/Cygwin platforms by detecting the
presence of Cygwin and using different compiler options consequently.
The documentation generation fits outside of the normal make process and is generated by the doxygen
tool itself.
We used Subversion (svn) [Tig] in order to manage the source code, makefile, and documentation revisions.
In Figure 2.5 and Figure 2.6, we show the example of function call and source file dependencies for a
function implementing vfprintf s.
On Implementation of a Safer C Library, ISO/IEC TR 24731. 11
2.6 Iterations
In order to efficiently reach our architectural goal, we divided the final objectives of the project in the
following steps:
In order to produce high-quality code, we decided to normalize on the OpenBSD style. We also decided to
use Doxygen [vH] source code documentation style for its completeness and the automated tool support.
Chapter 3
Implementation
This chapter gives concrete implementation details for the constraint handling API and examples of its
usage. An excessive amount of work was done to have a decent precondition validation and this chapter
mostly focuses on the API aspect of this implementatition. A particular achievement was to fully enable
parsing and restraining of %s and %n modifiers with the flex-based scanner.
For precondition validation we deviced an API to encasulate run-time constraint check and violation
information and an appropriate currently registered constraint handler. Later on this was abstracted with
inline function calls reducing the clutter for the libc s programmers such as ourselves and for those who
might maintain it after us.
The standard defines this type to allow custom constraint handlers in stdlib.h:
typedef void (* c o n s t r a i n t _ h a n d l e r _ t ) ( const char * restrict msg , void * restrict ptr , errno_t error ) ;
c o n s t r a i n t _ h a n d l e r _ t s e t _ c o n s t r a i n t _ h a n d l e r _ s ( c o n s t r a i n t _ h a n d l e r _ t handler ) ;
void a bo rt _h a nd le r_ s ( const char * restrict msg , void * restrict ptr , errno_t error ) ;
void i g n o r e _ h a n d l e r _ s ( const char * restrict msg , void * restrict ptr , errno_t error ) ;
In our implementation we mark abort handler s as the default handler for all contraint violations.
That means, after erroring out, the handler calls exit(0) and the application build around libc s termi-
nates.
This is the capsule enclosing the error information we defined. The structure depicts some meta information
about a paramater and its value. An instance of this structure is created for each input parameter to be
validated.
12
On Implementation of a Safer C Library, ISO/IEC TR 24731. 13
typedef struct _ p a r a m _ v a l i d a t i o n _ s t a t u s _ t
{
e_errcheck errtype ;
const void * value ;
const void * pairvalue ;
const void * result ;
const char * restrict function_name ;
const char * restrict param_name ;
const char * restrict pair_param_name ;
bool error_present ;
} param_validation_status_t ;
Some helper data structures allow us to describe more complex types. The param range t struct
allows specifying the range for a domain value. A reference to the instance of this struct is passed in
the param validation status t.pairvalue field. The object range t struct is supposed to contain the
data describing a memory objec with its starting address and length. The purpose of this is to help with
validation of ranged objects that they do not overlap in memory. The implementor of the library should
provide the two intances of this struct as references in value and pairvalue of the two ranged objects.
Which object reference goes to where is unimportant as the implementation takes care of figuring out the
object precedence.
The enumeration in Listing 3.5 defines most common error types to check for and report. These correspond
to the index for the human readable error messages.
typedef enum
{
E_NOERROR = 0 ,
E_NULL_PARAMETER_NOT_ALLOWED = 1,
E_PARAMETER_OUT_OF_RANGE = 2,
E_ENVIRONMENTAL_LIMIT_NOT_MET = 3,
E _ I N V A L I D _ F O R M A T _ P A R A M E T E R _ S = 4 , /* % s */
E _ I N V A L I D _ F O R M A T _ P A R A M E T E R _ N = 5 , /* % n */
E_RSIZE_MAX_EXCEEDED = 6,
E_NOT_ZERO = 7 ,
E_OBJECTS_OVERLAP = 8,
E_NOT_IMPLEMENTED = 9,
E _ T O K E N _ E N D _ N O T _ F O U N D = 10
} e_errcheck ;
Our implementation of the API (in Listing 3.6) does something similar to the code snippet presented in
Listing 3.7.
...
param_validation_status_t format_string_validation ;
_ _ m e m o r y _ z e r o _ f i l l _ r a n g e (& format_string_validation , sizeof ( p a r a m _ v a l i d a t i o n _ s t a t u s _ t ) ) ;
f o r m a t _ s t r i n g _ v a l i d a t i o n . errtype = E _ I N V A L I D _ F O R M A T _ P A R A M E T E R _ S ;
f o r m a t _ s t r i n g _ v a l i d a t i o n . value = format ;
f o r m a t _ s t r i n g _ v a l i d a t i o n . pairvalue = & arg ;
f o r m a t _ s t r i n g _ v a l i d a t i o n . param_name = " format / arg null % s " ;
f o r m a t _ s t r i n g _ v a l i d a t i o n . function_name = " vfprintf_s " ;
error = _ _ c o n t r a i n t _ v a l i d a t o r _ s (& f o r m a t _ s t r i n g _ v a l i d a t i o n ) ;
if ( error != OK )
{
errno = error ;
return error ;
}
...
Results
This chapter summarizes the result achieved as of this writing. This includes implemented API to this
point as well as some concrete results demonstraiting correctness of implementation.
This is the summary of the implemented API from the library and our internal constraint handling. We
summarized the functions and data types added in ISO/IEC TR 24731 in this section for the sake of
reference.
4.1.1 Library
Added the following data types: rsize t, errno t, constraint handler t that were necessary to add.
4.1.1.2 Functions
The functions in Listing 4.1 were to the large extend implemented by our team as of this writing. Likewise,
Listing 4.2 lists API not yet addressed. Finally, Listing 4.3 lists API implemented half-way through.
Added the following data types: param validation status t, param range t, object range t that were
necessary to add.
17
18 On Implementation of a Safer C Library, ISO/IEC TR 24731.
int fprintf_s ( FILE * restrict stream , const char * restrict format , ...) ;
int fscanf_s ( FILE * restrict stream , const char * restrict format , ...) ;
int printf_s ( const char * restrict format , ...) ;
int scanf_s ( const char * restrict format , ...) ;
int snprintf_s ( char * restrict s , rsize_t n , const char * restrict format , ...) ;
int sprintf_s ( char * restrict s , rsize_t n , const char * restrict format , ...) ;
int sscanf_s ( const char * restrict s , const char * restrict format , ...) ;
int vfprintf_s ( FILE * restrict stream , const char * restrict format , va_list arg ) ;
int vfscanf_s ( FILE * restrict stream , const char * restrict format , va_list arg ) ;
int vprintf_s ( const char * restrict format , va_list arg ) ;
int vscanf_s ( const char * restrict format , va_list arg ) ;
int vsnprintf_s ( char * restrict s , rsize_t n , const char * restrict format , va_list arg ) ;
int vsprintf_s ( char * restrict s , rsize_t n , const char * restrict format , va_list arg ) ;
int vsscanf_s ( const char * restrict s , const char * restrict format , va_list arg ) ;
c o n s t r a i n t _ h a n d l e r _ t s e t _ c o n s t r a i n t _ h a n d l e r _ s ( c o n s t r a i n t _ h a n d l e r _ t handler ) ;
void a bo rt _h a nd le r_ s ( const char * restrict msg , void * restrict ptr , errno_t error ) ;
void i g n o r e _ h a n d l e r _ s ( const char * restrict msg , void * restrict ptr , errno_t error ) ;
errno_t wctomb_s ( int * restrict status , char * restrict s , rsize_t smax , wchar_t wc ) ;
errno_t mbstowcs_s ( size_t * restrict retval , wchar_t * restrict dst , rsize_t dstmax , const char *
restrict src , rsize_t len ) ;
errno_t wcstombs_s ( size_t * restrict retval , char * restrict dst , rsize_t dstmax , const wchar_t *
restrict src , rsize_t len ) ;
errno_t memcpy_s ( void * restrict s1 , rsize_t s1max , const void * restrict s2 , rsize_t n ) ;
errno_t memmove_s ( void * s1 , rsize_t s1max , const void * s2 , rsize_t n ) ;
errno_t strcpy_s ( char * restrict s1 , rsize_t s1max , const char * restrict s2 ) ;
errno_t strncpy_s ( char * restrict s1 , rsize_t s1max , const char * restrict s2 , rsize_t n ) ;
errno_t strcat_s ( char * restrict s1 , rsize_t s1max , const char * restrict s2 ) ;
errno_t strncat_s ( char * restrict s1 , rsize_t s1max , const char * restrict s2 , rsize_t n ) ;
char * strtok_s ( char * restrict s1 , rsize_t * restrict s1max , const char * restrict s2 , char **
restrict ptr ) ;
int fwprintf_s ( FILE * restrict stream , const wchar_t * restrict format , ...) ;
int fwscanf_s ( FILE * restrict stream , const wchar_t * restrict format , ...) ;
int snwprintf_s ( wchar_t * restrict s , rsize_t n , const wchar_t * restrict format , ...) ;
int swprintf_s ( wchar_t * restrict s , rsize_t n , const wchar_t * restrict format , ...) ;
int swscanf_s ( const wchar_t * restrict s , const wchar_t * restrict format , ...) ;
int vfwprintf_s ( FILE * restrict stream , const wchar_t * restrict format , va_list arg ) ;
int vfwscanf_s ( FILE * restrict stream , const wchar_t * restrict format , va_list arg ) ;
int vsnwprintf_s ( wchar_t * restrict s , rsize_t n , const wchar_t * restrict format , va_list arg ) ;
int vswprintf_s ( wchar_t * restrict s , rsize_t n , const wchar_t * restrict format , va_list arg ) ;
int vswscanf_s ( const wchar_t * restrict s , const wchar_t * restrict format , va_list arg ) ;
int vwprintf_s ( const wchar_t * restrict format , va_list arg ) ;
int vwscanf_s ( const wchar_t * restrict format , va_list arg ) ;
int wprintf_s ( const wchar_t * restrict format , ...) ;
int wscanf_s ( const wchar_t * restrict format , ...) ;
/* c o n s t r a i n t v a l i d a t o r ; c o n s t r a i n t _ v a l i d a t o r _ s . c */
errno_t
_ _ c o n s t r a i n t _ v a l i d a t o r _ o b j e c t _ o v e r l a p ( const char * restrict function_name , const char * restrict
parameterNames , const void * restrict object1Start , const size_t object1Size , const void *
restrict object2Start , const size_t object2Size ) ;
errno_t
_ _ c o n s t r a i n t _ v a l i d a t o r _ v a l u e _ i n r a n g e ( const char * restrict function_name , const char * restrict
parameterName , const size_t value , const size_t lowerBound , const size_t upperBound ) ;
errno_t
_ _ c o n s t r a i n t _ v a l i d a t o r _ n o t _ n u l l ( const char * restrict function_name , const char * restrict
parameter_name , const void * restrict value_ptr ) ;
errno_t
_ _ c o n s t r a i n t _ v a l i d a t o r _ s _ f o r m a t ( const char * restrict function_name , const char * restrict
parameter_name , const char * restrict format , const va_list args ) ;
errno_t
_ _ c o n s t r a i n t _ v a l i d a t o r _ n _ f o r m a t ( const char * restrict function_name , const char * restrict
parameter_name , const char * restrict format , const va_list args ) ;
errno_t
_ _ c o n s t r a i n t _ v a l i d a t o r _ n o t _ z e r o ( const char * restrict function_name , const char * restrict
parameter_name , const size_t value ) ;
errno_t
_ _ c o n s t r a i n t _ v a l i d a t o r _ r s i z e _ l i m i t ( const char * restrict function_name , const char * restrict
parameter_name , const rsize_t value ) ;
void
_ _ r e p o r t _ c o n s t r a i n t _ v i o l a t i o n _ e n d _ o f _ t o k e n _ n o t _ p r e s e n t ( const char * restrict function_name , const
char * restrict paramete r_name ) ;
errno_t
_ _ c o n s t r a i n t _ v a l i d a t o r _ n o t _ n u l l _ a r g s ( const char * restrict function_name , const char * restrict
parameter_name , const char * restrict format , const va_list args ) ;
4.1.2.2 Functions
The functions in Listing 4.4 were to the large extend implemented by our team as of this writing. Likewise,
Listing 4.5 lists API not yet addressed.
/*
* Sloppy P r o g r a m m i n g Test Cases
*/
# define _ _ S T D C _ W A N T _ L I B _ E X T 1 _ _ 1
# include " stdio . h "
# include " stdlib . h "
int
main ( int argc , char ** argv )
{
int valid = 1;
set_constraint_handler_s ( ignore_handler_s );
if ( argc > 1)
{
printf ( " Sloppy programming zone : [[% s ]]\ n " , argv [1]) ;
printf_s ( argv [1]) ;
printf ( " \ n \ n " ) ;
}
printf ( " return value for %% n : [% d ]\ n " , printf_s ( " % n \ n " , & argc ) ) ;
printf ( " return value for %% s : [% d ]\ n " , printf_s ( " % s \ n " , NULL ) ) ;
return 0;
}
/* EOF */
Listing 4.6: Example of a Test Program for stdio to test and reject invalid %s and %n cases.
22 On Implementation of a Safer C Library, ISO/IEC TR 24731.
valid s = [% s ]
valid s = [ valid ]
valid n1 = [ n ]
valid n1 = [%% n ]
valid n3 = [% n ]
# define _ _ S T D C _ W A N T _ L I B _ E X T 1 _ _ 1
# include " string . h "
# include < errno .h >
# include < stdio .h >
# include < string .h >
# include " stdlib . h "
set_constraint_handler_s ( ignore_handler_s );
/* Failure tests */
printf ( " strcpy_s failure test :\ t " ) ;
strcpy_s ( buffer1 , 1024 , NULL ) ;
printf ( " strncpy_s failure test :\ t " ) ;
strncpy_s ( buffer1 , 10 , buffer2 , 50) ;
printf ( " strncat_s failure test :\ t " ) ;
strcat_s ( buffer1 , -1 , buffer2 ) ;
printf ( " strncat_s failure test :\ t " ) ;
strncat_s ( NULL , 1024 , NULL , 50) ;
printf ( " strncat_s failure test :\ t " ) ;
strncat_s ( buffer1 , 1024 , buffer1 -10 , 50) ;
printf ( " memcpy_s failure test :\ t " ) ;
memcpy_s ( buffer1 , 1024 , buffer2 , -1) ;
printf ( " memmove_s failure test :\ t " ) ;
memmove_s ( buffer1 , 1023 , NULL , 1024) ;
/* Normal o p e r a t i o n tests */
strcpy_s ( buffer1 , 1024 , " test string " ) ;
printf ( " strcpy_s : % s \ n " , buffer1 ) ;
strncpy_s ( buffer2 , 1024 , buffer1 , 1024) ;
printf ( " strncpy_s : % s \ n " , buffer2 ) ;
strcat_s ( buffer1 , 1024 , buffer2 ) ;
printf ( " strcat_s : % s \ n " , buffer1 ) ;
...
printf ( " strnlen_s ( buffer1 ) : % u \ n " , strlength ) ;
size_t errlen = strerrorlen_s ( EINVAL ) ;
printf ( " strerrorlen_s ( EINVAL ) : % u \ n " , errlen ) ;
strerror_s ( buffer2 , 1014 , EINVAL ) ;
printf ( " % s \ n " , buffer2 ) ;
rsize_t l = strnlen_s ( buffer1 , 100) ;
char * strktokresult = NULL ;
char * token = strtok_s ( buffer1 , &l , " " , & strktokresult ) ;
printf ( " strtok_s token : %s , remaining length : %u , remaining substring : % s \ n " , token , l ,
strktokresult ) ;
token = strtok_s ( NULL , &l , " gt " , & strktokresult ) ; /* Should be found */
...
/* Move printfs to a real test file */
size_t s = strnlen_s ( " 12345 " , 10) ;
printf ( " size : % u \ n " , s ) ;
}
Listing 4.8: Example of a Test Program for string to test and reject invalid cases.
24 On Implementation of a Safer C Library, ISO/IEC TR 24731.
Conclusions
• Difficulties
• Limitations
• Acknowledgments
• Future Work
5.3 Acknowledgments
• ISO
This project, being a derivative of the standard C library, will see a major effort put into the testing of
the library. Furthermore, a large part of the project (transforming the obsolete calls to wrappers to the
newer ones) makes the assumption that the buffer free size is easily obtainable, an assumption which may
not necessarily hold true in all circumstances. As such, it is possible that we will have to develop novel
algorithms to ensure that this portability is possible, or it may also be that this portability is not 100%
attainable. Furthermore, we will need to investigate good potential software for performance and security
testing of our improved solution. Thus, we will focus on:
• Completion of implementation,
• Addition more comprehensive test cases by the developers and the OSS community,
[HNS00] Christine Hofmeister, Robert Nord, and Dilip Soni. Applied Software Architecture. Addison-
Wesley, 2000.
[ISO05] ISO/IEC. ISO/IEC Directives, Part 2: Rules for the structure and drafting of International
Standards, 5th edition, 2005. http://isotc.iso.org/livelink/livelink.exe/4230517/
ISO IEC Directives Part 2 Rules for the structure and drafting of International
Standards 2004 5th edition pdf format .pdf?func=doc.Fetch&nodeid=4230517.
[MdR99] T. C. Miller and T. de Raadt. strlcpy and strlcatconsistent, safe string copy and concatenation. In
Proceedings of the FREENIX Track, 1999 USENIX Annual Technical Conference, pages 175–178.
USENIX Association, 1999. http://www.usenix.org/publications/library/proceedings/
usenix99/full papers/millert/millert.pdf.
[PMB] M-A Laverdière Papineau, S. Mokhov, and D. Benredjem. Statistical classification of vulnerabil-
ity solutions in the linux kernels 2.4/2.6. Submitted to IEEE COMPSAC, pending acceptance.
[SC2] ISO/IEC JTC1 SC22/WG14. Draft minutes for 25-28 september 2005 meeting of iso/iec jtc1
sc22/wg14 and incits j11. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1145.pdf.
[Sea05] R. Seacord. Secure Coding in C and C++. SEI Series. Addison-Wesley, 2005.
[vH] Dimitry van Heesch. Doxygen manual for version 1.4.6. ftp://ftp.stack.nl/pub/users/
dimitri/doxygen manual-1.4.6.pdf.zip.
[WG106] ISO/IEC JTC1 SC22 WG14. Programming language c - specification for safer more secure c
library functions. Technical Report ISO/IEC TR 24731, ISO, 2006. Draft status at time of
writing.
Index
API Library, 17
abort handler s, 12 Private Constraint Handling, 17
constraint handler t, 17 Introduction, 1
errno t, 17
Methodology
exit(0), 12
Principles and Philosophy, 3
object range t, 13, 17
pairvalue, 13 Results, 17
param range t, 13, 17
param validation status t, 13, 17 Siemens Four View Model, 4
param validation status t.pairvalue, 13 Code View, 4
rsize t, 17 Conceptual View, 4
strlcpy, 1 Execution View, 4
strtok s, 25 Module View, 4
value, 13
Tools
Architecture, 3
libc s, 12
Conclusions, 25 svn, 10
Files
.a, 10
.h, 7
.o, 10
.so, 10
adapters, 10
include, 7
makefile, 10
stdio.h, 7
stdlib.h, 7, 12
string.h, 7
strsafe.h, 1
time.h, 7
wchar.h, 7
Implementation, 12
Implemented API, 17
29