mirror of
https://github.com/postgres/postgres.git
synced 2025-12-21 05:21:08 +03:00
This commit addresses some issues with c3826f83 that moved the hex
decoding routine to src/common/:
- The decoding function lacked overflow checks, so when used for
security-related features it was an open door to out-of-bound writes if
not carefully used that could remain undetected. Like the base64
routines already in src/common/ used by SCRAM, this routine is reworked
to check for overflows by having the size of the destination buffer
passed as argument, with overflows checked before doing any writes.
- The encoding routine was missing. This is moved to src/common/ and
it gains the same overflow checks as the decoding part.
On failure, the hex routines of src/common/ issue an error as per the
discussion done to make them usable by frontend tools, but not by shared
libraries. Note that this is why ECPG is left out of this commit, and
it still includes a duplicated logic doing hex encoding and decoding.
While on it, this commit uses better variable names for the source and
destination buffers in the existing escape and base64 routines in
encode.c and it makes them more robust to overflow detection. The
previous core code issued a FATAL after doing out-of-bound writes if
going through the SQL functions, which would be enough to detect
problems when working on changes that impacted this area of the
code. Instead, an error is issued before doing an out-of-bound write.
The hex routines were being directly called for bytea conversions and
backup manifests without such sanity checks. The current calls happen
to not have any problems, but careless uses of such APIs could easily
lead to CVE-class bugs.
Author: Bruce Momjian, Michael Paquier
Reviewed-by: Sehrope Sarkuni
Discussion: https://postgr.es/m/20201231003557.GB22199@momjian.us
124 lines
4.2 KiB
C
124 lines
4.2 KiB
C
/*-------------------------------------------------------------------------
|
|
*
|
|
* builtins.h
|
|
* Declarations for operations on built-in types.
|
|
*
|
|
*
|
|
* Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
*
|
|
* src/include/utils/builtins.h
|
|
*
|
|
*-------------------------------------------------------------------------
|
|
*/
|
|
#ifndef BUILTINS_H
|
|
#define BUILTINS_H
|
|
|
|
#include "fmgr.h"
|
|
#include "nodes/nodes.h"
|
|
#include "utils/fmgrprotos.h"
|
|
|
|
/* Sign + the most decimal digits an 8-byte number could have */
|
|
#define MAXINT8LEN 20
|
|
|
|
/* bool.c */
|
|
extern bool parse_bool(const char *value, bool *result);
|
|
extern bool parse_bool_with_len(const char *value, size_t len, bool *result);
|
|
|
|
/* domains.c */
|
|
extern void domain_check(Datum value, bool isnull, Oid domainType,
|
|
void **extra, MemoryContext mcxt);
|
|
extern int errdatatype(Oid datatypeOid);
|
|
extern int errdomainconstraint(Oid datatypeOid, const char *conname);
|
|
|
|
/* int.c */
|
|
extern int2vector *buildint2vector(const int16 *int2s, int n);
|
|
|
|
/* name.c */
|
|
extern void namestrcpy(Name name, const char *str);
|
|
extern int namestrcmp(Name name, const char *str);
|
|
|
|
/* numutils.c */
|
|
extern int32 pg_atoi(const char *s, int size, int c);
|
|
extern int16 pg_strtoint16(const char *s);
|
|
extern int32 pg_strtoint32(const char *s);
|
|
extern int pg_itoa(int16 i, char *a);
|
|
extern int pg_ultoa_n(uint32 l, char *a);
|
|
extern int pg_ulltoa_n(uint64 l, char *a);
|
|
extern int pg_ltoa(int32 l, char *a);
|
|
extern int pg_lltoa(int64 ll, char *a);
|
|
extern char *pg_ultostr_zeropad(char *str, uint32 value, int32 minwidth);
|
|
extern char *pg_ultostr(char *str, uint32 value);
|
|
extern uint64 pg_strtouint64(const char *str, char **endptr, int base);
|
|
|
|
/* oid.c */
|
|
extern oidvector *buildoidvector(const Oid *oids, int n);
|
|
extern Oid oidparse(Node *node);
|
|
extern int oid_cmp(const void *p1, const void *p2);
|
|
|
|
/* regexp.c */
|
|
extern char *regexp_fixed_prefix(text *text_re, bool case_insensitive,
|
|
Oid collation, bool *exact);
|
|
|
|
/* ruleutils.c */
|
|
extern bool quote_all_identifiers;
|
|
extern const char *quote_identifier(const char *ident);
|
|
extern char *quote_qualified_identifier(const char *qualifier,
|
|
const char *ident);
|
|
extern void generate_operator_clause(fmStringInfo buf,
|
|
const char *leftop, Oid leftoptype,
|
|
Oid opoid,
|
|
const char *rightop, Oid rightoptype);
|
|
|
|
/* varchar.c */
|
|
extern int bpchartruelen(char *s, int len);
|
|
|
|
/* popular functions from varlena.c */
|
|
extern text *cstring_to_text(const char *s);
|
|
extern text *cstring_to_text_with_len(const char *s, int len);
|
|
extern char *text_to_cstring(const text *t);
|
|
extern void text_to_cstring_buffer(const text *src, char *dst, size_t dst_len);
|
|
|
|
#define CStringGetTextDatum(s) PointerGetDatum(cstring_to_text(s))
|
|
#define TextDatumGetCString(d) text_to_cstring((text *) DatumGetPointer(d))
|
|
|
|
/* xid.c */
|
|
extern int xidComparator(const void *arg1, const void *arg2);
|
|
|
|
/* inet_cidr_ntop.c */
|
|
extern char *pg_inet_cidr_ntop(int af, const void *src, int bits,
|
|
char *dst, size_t size);
|
|
|
|
/* inet_net_pton.c */
|
|
extern int pg_inet_net_pton(int af, const char *src,
|
|
void *dst, size_t size);
|
|
|
|
/* network.c */
|
|
extern double convert_network_to_scalar(Datum value, Oid typid, bool *failure);
|
|
extern Datum network_scan_first(Datum in);
|
|
extern Datum network_scan_last(Datum in);
|
|
extern void clean_ipv6_addr(int addr_family, char *addr);
|
|
|
|
/* numeric.c */
|
|
extern Datum numeric_float8_no_overflow(PG_FUNCTION_ARGS);
|
|
|
|
/* format_type.c */
|
|
|
|
/* Control flags for format_type_extended */
|
|
#define FORMAT_TYPE_TYPEMOD_GIVEN 0x01 /* typemod defined by caller */
|
|
#define FORMAT_TYPE_ALLOW_INVALID 0x02 /* allow invalid types */
|
|
#define FORMAT_TYPE_FORCE_QUALIFY 0x04 /* force qualification of type */
|
|
#define FORMAT_TYPE_INVALID_AS_NULL 0x08 /* NULL if undefined */
|
|
extern char *format_type_extended(Oid type_oid, int32 typemod, bits16 flags);
|
|
|
|
extern char *format_type_be(Oid type_oid);
|
|
extern char *format_type_be_qualified(Oid type_oid);
|
|
extern char *format_type_with_typemod(Oid type_oid, int32 typemod);
|
|
|
|
extern int32 type_maximum_size(Oid type_oid, int32 typemod);
|
|
|
|
/* quote.c */
|
|
extern char *quote_literal_cstr(const char *rawstr);
|
|
|
|
#endif /* BUILTINS_H */
|