Fix undefined function pointer casts in IMPLEMENT_PEM_*.

While it is okay to cast function pointers into different types for
generic storage, the pointer must be cast back to the exact same type
when calling. In particular, although C libraries do this sort of thing
all the time, calling a T* d2i function as a void* d2i function is
undefined:

  If the function is defined with a type that is not compatible with the
  type (of the expression) pointed to by the expression that denotes the
  called function, the behavior is undefined

Fix some instances in the PEM/ASN1 wrapper functions. Synthesize helper
functions instead.

This CL just addresses the function pointer issues. The inherited legacy
OpenSSL ASN.1 code is still full other questionable data pointer dances
that will be much more difficult to excise. Continuing to exise that
code altogether (it is already unshipped from Cronet and unshipped from
Chrome but for WebRTC) is probably a better tack there.

This removes one (of many many) places where we require
-fsanitize-cfi-icall-generalize-pointers.

Bug: chromium:785442
Change-Id: Id8056ead6ef471f0fdf263bb50dc659da500e8ce
Reviewed-on: https://boringssl-review.googlesource.com/32105
Reviewed-by: Adam Langley <agl@google.com>
Reviewed-by: Adam Langley <alangley@gmail.com>
This commit is contained in:
David Benjamin 2018-09-22 16:49:56 -05:00 committed by Adam Langley
parent 3474270abd
commit 217bfd3c96

View File

@ -124,71 +124,107 @@ extern "C" {
#else #else
#define IMPLEMENT_PEM_read_fp(name, type, str, asn1) \ #define IMPLEMENT_PEM_read_fp(name, type, str, asn1) \
OPENSSL_EXPORT type *PEM_read_##name(FILE *fp, type **x, pem_password_cb *cb, void *u)\ static void *pem_read_##name##_d2i(void **x, const unsigned char **inp, \
{ \ long len) { \
return (type *)PEM_ASN1_read((d2i_of_void *)d2i_##asn1, str,fp,(void **)x,cb,u); \ return d2i_##asn1((type **)x, inp, len); \
} \
OPENSSL_EXPORT type *PEM_read_##name(FILE *fp, type **x, \
pem_password_cb *cb, void *u) { \
return (type *)PEM_ASN1_read(pem_read_##name##_d2i, str, fp, (void **)x, \
cb, u); \
} }
#define IMPLEMENT_PEM_write_fp(name, type, str, asn1) \ #define IMPLEMENT_PEM_write_fp(name, type, str, asn1) \
OPENSSL_EXPORT int PEM_write_##name(FILE *fp, type *x) \ static int pem_write_##name##_i2d(const void *x, unsigned char **outp) { \
{ \ return i2d_##asn1((type *)x, outp); \
return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,x,NULL,NULL,0,NULL,NULL); \ } \
OPENSSL_EXPORT int PEM_write_##name(FILE *fp, type *x) { \
return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, NULL, NULL, 0, \
NULL, NULL); \
} }
#define IMPLEMENT_PEM_write_fp_const(name, type, str, asn1) \ #define IMPLEMENT_PEM_write_fp_const(name, type, str, asn1) \
OPENSSL_EXPORT int PEM_write_##name(FILE *fp, const type *x) \ static int pem_write_##name##_i2d(const void *x, unsigned char **outp) { \
{ \ return i2d_##asn1((const type *)x, outp); \
return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,(void *)x,NULL,NULL,0,NULL,NULL); \ } \
OPENSSL_EXPORT int PEM_write_##name(FILE *fp, const type *x) { \
return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, (void *)x, NULL, \
NULL, 0, NULL, NULL); \
} }
#define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) \ #define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) \
OPENSSL_EXPORT int PEM_write_##name(FILE *fp, type *x, const EVP_CIPHER *enc, \ static int pem_write_##name##_i2d(const void *x, unsigned char **outp) { \
unsigned char *kstr, int klen, pem_password_cb *cb, \ return i2d_##asn1((type *)x, outp); \
void *u) \ } \
{ \ OPENSSL_EXPORT int PEM_write_##name( \
return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,x,enc,kstr,klen,cb,u); \ FILE *fp, type *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, \
pem_password_cb *cb, void *u) { \
return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, enc, kstr, klen, \
cb, u); \
} }
#define IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1) \ #define IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1) \
OPENSSL_EXPORT int PEM_write_##name(FILE *fp, type *x, const EVP_CIPHER *enc, \ static int pem_write_##name##_i2d(const void *x, unsigned char **outp) { \
unsigned char *kstr, int klen, pem_password_cb *cb, \ return i2d_##asn1((const type *)x, outp); \
void *u) \ } \
{ \ OPENSSL_EXPORT int PEM_write_##name( \
return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,x,enc,kstr,klen,cb,u); \ FILE *fp, type *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, \
pem_password_cb *cb, void *u) { \
return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, enc, kstr, klen, \
cb, u); \
} }
#endif #endif
#define IMPLEMENT_PEM_read_bio(name, type, str, asn1) \ #define IMPLEMENT_PEM_read_bio(name, type, str, asn1) \
OPENSSL_EXPORT type *PEM_read_bio_##name(BIO *bp, type **x, pem_password_cb *cb, void *u)\ static void *pem_read_bio_##name##_d2i(void **x, const unsigned char **inp, \
{ \ long len) { \
return (type *)PEM_ASN1_read_bio((d2i_of_void *)d2i_##asn1, str,bp,(void **)x,cb,u); \ return d2i_##asn1((type **)x, inp, len); \
} \
OPENSSL_EXPORT type *PEM_read_bio_##name(BIO *bp, type **x, \
pem_password_cb *cb, void *u) { \
return (type *)PEM_ASN1_read_bio(pem_read_bio_##name##_d2i, str, bp, \
(void **)x, cb, u); \
} }
#define IMPLEMENT_PEM_write_bio(name, type, str, asn1) \ #define IMPLEMENT_PEM_write_bio(name, type, str, asn1) \
OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, type *x) \ static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \
{ \ return i2d_##asn1((type *)x, outp); \
return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,x,NULL,NULL,0,NULL,NULL); \ } \
OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, type *x) { \
return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, x, NULL, \
NULL, 0, NULL, NULL); \
} }
#define IMPLEMENT_PEM_write_bio_const(name, type, str, asn1) \ #define IMPLEMENT_PEM_write_bio_const(name, type, str, asn1) \
OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, const type *x) \ static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \
{ \ return i2d_##asn1((const type *)x, outp); \
return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,(void *)x,NULL,NULL,0,NULL,NULL); \ } \
OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, const type *x) { \
return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, (void *)x, \
NULL, NULL, 0, NULL, NULL); \
} }
#define IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \ #define IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \
OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, type *x, const EVP_CIPHER *enc, \ static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \
unsigned char *kstr, int klen, pem_password_cb *cb, void *u) \ return i2d_##asn1((type *)x, outp); \
{ \ } \
return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,x,enc,kstr,klen,cb,u); \ OPENSSL_EXPORT int PEM_write_bio_##name( \
BIO *bp, type *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, \
pem_password_cb *cb, void *u) { \
return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, x, enc, \
kstr, klen, cb, u); \
} }
#define IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1) \ #define IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1) \
OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, type *x, const EVP_CIPHER *enc, \ static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \
unsigned char *kstr, int klen, pem_password_cb *cb, void *u) \ return i2d_##asn1((const type *)x, outp); \
{ \ } \
return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,(void *)x,enc,kstr,klen,cb,u); \ OPENSSL_EXPORT int PEM_write_bio_##name( \
BIO *bp, type *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, \
pem_password_cb *cb, void *u) { \
return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, (void *)x, \
enc, kstr, klen, cb, u); \
} }
#define IMPLEMENT_PEM_write(name, type, str, asn1) \ #define IMPLEMENT_PEM_write(name, type, str, asn1) \