Skip to content

Commit

Permalink
global: Change string position/length from unsigned int to size_t
Browse files Browse the repository at this point in the history
Mainly to avoid truncating >4GB strings, which might potentially cause
some security holes. Normally there are other limits, which prevent such
excessive strings from being created in the first place.

I'm sure this didn't find everything. Maybe everything could be found with
compiler warnings. -Wconversion kind of does it, but it gives way too many
unnecessary warnings.

These were mainly found with:

grep " = strlen"
egrep "unsigned int.*(size|len)"
  • Loading branch information
sirainen committed Dec 16, 2016
1 parent 19b9eb5 commit 2ac5f36
Show file tree
Hide file tree
Showing 143 changed files with 310 additions and 267 deletions.
2 changes: 1 addition & 1 deletion src/auth/auth-cache.c
Expand Up @@ -271,7 +271,7 @@ static bool auth_cache_node_is_user(struct auth_cache_node *node,
const char *username)
{
const char *data = node->data;
unsigned int username_len;
size_t username_len;

/* The cache nodes begin with "P"/"U", passdb/userdb ID, optional
"+" master user, "\t" and then usually followed by the username.
Expand Down
4 changes: 2 additions & 2 deletions src/auth/auth-request.c
Expand Up @@ -1745,7 +1745,7 @@ void auth_request_set_field(struct auth_request *request,
const char *name, const char *value,
const char *default_scheme)
{
unsigned int name_len = strlen(name);
size_t name_len = strlen(name);

i_assert(*name != '\0');
i_assert(value != NULL);
Expand Down Expand Up @@ -1966,7 +1966,7 @@ auth_request_userdb_import(struct auth_request *request, const char *args)
void auth_request_set_userdb_field(struct auth_request *request,
const char *name, const char *value)
{
unsigned int name_len = strlen(name);
size_t name_len = strlen(name);
uid_t uid;
gid_t gid;

Expand Down
2 changes: 1 addition & 1 deletion src/auth/auth-request.h
Expand Up @@ -81,7 +81,7 @@ struct auth_request {
struct timeout *to_abort, *to_penalty;
unsigned int policy_penalty;
unsigned int last_penalty;
unsigned int initial_response_len;
size_t initial_response_len;
const unsigned char *initial_response;

union {
Expand Down
6 changes: 3 additions & 3 deletions src/auth/db-checkpassword.c
Expand Up @@ -35,7 +35,7 @@ struct chkpw_auth_request {
struct io *io_out, *io_in;

string_t *input_buf;
unsigned int output_pos, output_len;
size_t output_pos, output_len;

int exit_status;
bool exited:1;
Expand Down Expand Up @@ -463,7 +463,7 @@ void db_checkpassword_call(struct db_checkpassword *db,
void (*request_callback)())
{
struct chkpw_auth_request *chkpw_auth_request;
unsigned int output_len;
size_t output_len;
int fd_in[2], fd_out[2];
pid_t pid;

Expand All @@ -473,7 +473,7 @@ void db_checkpassword_call(struct db_checkpassword *db,
output_len += strlen(auth_password);
if (output_len > CHECKPASSWORD_MAX_REQUEST_LEN) {
auth_request_log_info(request, AUTH_SUBSYS_DB,
"Username+password combination too long (%u bytes)",
"Username+password combination too long (%"PRIuSIZE_T" bytes)",
output_len);
callback(request, DB_CHECKPASSWORD_STATUS_FAILURE,
NULL, request_callback);
Expand Down
3 changes: 2 additions & 1 deletion src/auth/db-ldap.c
Expand Up @@ -1797,7 +1797,8 @@ db_ldap_result_finish_debug(struct db_ldap_result_iterate_context *ctx)
struct hash_iterate_context *iter;
char *name;
struct db_ldap_value *value;
unsigned int orig_len, unused_count = 0;
unsigned int unused_count = 0;
size_t orig_len;

orig_len = str_len(ctx->debug);
if (orig_len == 0) {
Expand Down
6 changes: 3 additions & 3 deletions src/auth/mech-gssapi.c
Expand Up @@ -233,10 +233,10 @@ duplicate_name(struct auth_request *request, gss_name_t old)
return new;
}

static bool data_has_nuls(const void *data, unsigned int len)
static bool data_has_nuls(const void *data, size_t len)
{
const unsigned char *c = data;
unsigned int i;
size_t i;

/* apparently all names end with NUL? */
if (len > 0 && c[len-1] == '\0')
Expand Down Expand Up @@ -584,7 +584,7 @@ mech_gssapi_unwrap(struct gssapi_auth_request *request, gss_buffer_desc inbuf)
gss_buffer_desc outbuf;
const char *login_user, *error;
unsigned char *name;
unsigned int name_len;
size_t name_len;

major_status = gss_unwrap(&minor_status, request->gss_ctx,
&inbuf, &outbuf, NULL, NULL);
Expand Down
2 changes: 1 addition & 1 deletion src/auth/mech-scram-sha1.c
Expand Up @@ -350,7 +350,7 @@ static void mech_scram_sha1_auth_continue(struct auth_request *auth_request,
(struct scram_auth_request *)auth_request;
const char *error = NULL;
const char *server_final_message;
int len;
size_t len;

if (request->client_first_message_bare == NULL) {
/* Received client-first-message */
Expand Down
6 changes: 3 additions & 3 deletions src/auth/password-scheme.c
Expand Up @@ -44,7 +44,7 @@ password_scheme_lookup(const char *name, enum password_encoding *encoding_r)
{
const struct password_scheme *scheme;
const char *encoding = NULL;
unsigned int scheme_len;
size_t scheme_len;

*encoding_r = PW_ENCODING_NONE;

Expand Down Expand Up @@ -145,7 +145,7 @@ int password_decode(const char *password, const char *scheme,
const struct password_scheme *s;
enum password_encoding encoding;
buffer_t *buf;
unsigned int len;
size_t len;
bool guessed_encoding;

*error_r = NULL;
Expand Down Expand Up @@ -614,7 +614,7 @@ plain_trunc_verify(const char *plaintext, const char *user ATTR_UNUSED,
const unsigned char *raw_password, size_t size,
const char **error_r)
{
unsigned int i, plaintext_len, trunc_len = 0;
size_t i, plaintext_len, trunc_len = 0;

/* format: <length>-<password> */
for (i = 0; i < size; i++) {
Expand Down
2 changes: 1 addition & 1 deletion src/auth/userdb-dict.c
Expand Up @@ -22,7 +22,7 @@ struct dict_userdb_iterate_context {

userdb_callback_t *userdb_callback;
const char *key_prefix;
unsigned int key_prefix_len;
size_t key_prefix_len;
struct dict_iterate_context *iter;
};

Expand Down
4 changes: 2 additions & 2 deletions src/config/config-parser-private.h
Expand Up @@ -23,7 +23,7 @@ struct config_section_stack {
struct config_filter filter;
/* root=NULL-terminated list of parsers */
struct config_module_parser *parsers;
unsigned int pathlen;
size_t pathlen;

const char *open_path;
unsigned int open_linenum;
Expand All @@ -48,7 +48,7 @@ struct config_parser_context {
struct input_stack *cur_input;

string_t *str;
unsigned int pathlen;
size_t pathlen;
unsigned int section_counter;
const char *error;

Expand Down
2 changes: 1 addition & 1 deletion src/config/config-parser.c
Expand Up @@ -552,7 +552,7 @@ config_parse_line(struct config_parser_context *ctx,
const char **key_r, const char **value_r)
{
const char *key;
unsigned int len;
size_t len;
char *p;

*key_r = NULL;
Expand Down
5 changes: 3 additions & 2 deletions src/config/config-request.c
Expand Up @@ -166,7 +166,7 @@ bool config_export_type(string_t *str, const void *value,
}
case SET_ENUM: {
const char *const *val = value;
unsigned int len = strlen(*val);
size_t len = strlen(*val);

if (dump_default)
str_append(str, *val);
Expand Down Expand Up @@ -220,7 +220,8 @@ settings_export(struct config_export_context *ctx,
const struct setting_define *def;
const void *value, *default_value, *change_value;
void *const *children, *const *change_children = NULL;
unsigned int i, count, count2, prefix_len;
unsigned int i, count, count2;
size_t prefix_len;
const char *str;
char *key;
bool dump, dump_default = FALSE;
Expand Down
16 changes: 9 additions & 7 deletions src/config/doveconf.c
Expand Up @@ -154,7 +154,7 @@ static void config_dump_human_deinit(struct config_dump_human_context *ctx)

static bool value_need_quote(const char *value)
{
unsigned int len = strlen(value);
size_t len = strlen(value);

if (len == 0)
return FALSE;
Expand All @@ -176,8 +176,9 @@ config_dump_human_output(struct config_dump_human_context *ctx,
struct prefix_stack prefix;
const char *const *strings, *const *args, *p, *str, *const *prefixes;
const char *key, *key2, *value;
unsigned int i, j, count, len, prefix_count, skip_len;
unsigned int setting_name_filter_len, prefix_idx = UINT_MAX;
unsigned int i, j, count, prefix_count;
unsigned int prefix_idx = UINT_MAX;
size_t len, skip_len, setting_name_filter_len;
bool unique_key;
int ret = 0;

Expand Down Expand Up @@ -461,7 +462,7 @@ config_dump_one(const struct config_filter *filter, bool hide_key,
{
static struct config_dump_human_context *ctx;
const char *const *str;
unsigned int len;
size_t len;
bool dump_section = FALSE;

ctx = config_dump_human_init(NULL, scope, FALSE);
Expand Down Expand Up @@ -499,7 +500,8 @@ static void config_request_simple_stdout(const char *key, const char *value,
void *context)
{
char **setting_name_filters = context;
unsigned int i, filter_len;
unsigned int i;
size_t filter_len;

if (setting_name_filters == NULL) {
printf("%s=%s\n", key, value);
Expand Down Expand Up @@ -600,7 +602,7 @@ static void hostname_verify_format(const char *arg)
struct hostname_format fmt;
const char *p;
unsigned char hash[GUID_128_HOST_HASH_SIZE];
unsigned int len, n, limit;
unsigned int n, limit;
HASH_TABLE(void *, void *) hosts;
void *key, *value;
string_t *host;
Expand All @@ -627,7 +629,7 @@ static void hostname_verify_format(const char *arg)
fmt.suffix = p;
} else {
/* detect host1[suffix] vs host01[suffix] */
len = strlen(my_hostname);
size_t len = strlen(my_hostname);
while (len > 0 && !i_isdigit(my_hostname[len-1]))
len--;
fmt.suffix = my_hostname + len;
Expand Down
4 changes: 2 additions & 2 deletions src/config/old-set-parser.c
Expand Up @@ -150,7 +150,7 @@ old_settings_handle_root(struct config_parser_context *ctx,
const char *key, const char *value)
{
const char *p;
unsigned int len;
size_t len;

if (strcmp(key, "base_dir") == 0) {
len = strlen(value);
Expand Down Expand Up @@ -595,7 +595,7 @@ static void socket_apply(struct config_parser_context *ctx)
{
const struct socket_set *set = &ctx->old->socket_set;
const char *path, *prefix;
unsigned int len;
size_t len;
bool master_suffix;

if (set->path == NULL) {
Expand Down
2 changes: 1 addition & 1 deletion src/director/director-connection.c
Expand Up @@ -2145,7 +2145,7 @@ static void director_connection_reconnect(struct director_connection **_conn,
void director_connection_send(struct director_connection *conn,
const char *data)
{
unsigned int len = strlen(data);
size_t len = strlen(data);
off_t ret;

if (conn->output->closed || !conn->connected)
Expand Down
2 changes: 1 addition & 1 deletion src/doveadm/doveadm-auth-server.c
Expand Up @@ -87,7 +87,7 @@ cmd_user_input(struct auth_master_connection *conn,
t_strdup_printf("\"error\":\"%s: user doesn't exist\"",
lookup_name));
} else if (show_field != NULL) {
unsigned int show_field_len = strlen(show_field);
size_t show_field_len = strlen(show_field);
string_t *json_field = t_str_new(show_field_len+1);
json_append_escaped(json_field, show_field);
o_stream_nsend_str(doveadm_print_ostream, t_strdup_printf("\"%s\":", str_c(json_field)));
Expand Down
2 changes: 1 addition & 1 deletion src/doveadm/doveadm-auth.c
Expand Up @@ -86,7 +86,7 @@ cmd_user_input(struct auth_master_connection *conn,
"%s: user %s doesn't exist\n", lookup_name,
input->username);
} else if (show_field != NULL) {
unsigned int show_field_len = strlen(show_field);
size_t show_field_len = strlen(show_field);

for (; *fields != NULL; fields++) {
if (strncmp(*fields, show_field, show_field_len) == 0 &&
Expand Down
4 changes: 2 additions & 2 deletions src/doveadm/doveadm-cmd.c
Expand Up @@ -111,7 +111,7 @@ doveadm_cmd_find_multi_word(const char *cmdname, int *_argc,
{
int argc = *_argc;
const char *const *argv = *_argv;
unsigned int len;
size_t len;

if (argc < 2)
return FALSE;
Expand Down Expand Up @@ -141,7 +141,7 @@ doveadm_cmd_find_with_args(const char *cmd_name, int *argc,
const char *const *argv[])
{
const struct doveadm_cmd *cmd;
unsigned int cmd_name_len;
size_t cmd_name_len;

i_assert(*argc > 0);

Expand Down
2 changes: 1 addition & 1 deletion src/doveadm/doveadm-fs.c
Expand Up @@ -385,7 +385,7 @@ static void cmd_fs_delete_recursive_path(struct fs *fs, const char *path,
unsigned int async_count)
{
struct fs_file *file;
unsigned int path_len;
size_t path_len;

path_len = strlen(path);
if (path_len > 0 && path[path_len-1] != '/')
Expand Down
7 changes: 4 additions & 3 deletions src/doveadm/doveadm-log.c
Expand Up @@ -91,7 +91,7 @@ cmd_log_find_syslog_files(struct log_find_context *ctx, const char *path)
struct stat st;
char *key;
string_t *full_path;
unsigned int dir_len;
size_t dir_len;

dir = opendir(path);
if (dir == NULL) {
Expand Down Expand Up @@ -131,7 +131,8 @@ cmd_log_find_syslog_files(struct log_find_context *ctx, const char *path)

static bool log_type_find(const char *str, enum log_type *type_r)
{
unsigned int i, len = strlen(str);
unsigned int i;
size_t len = strlen(str);

for (i = 0; i < LAST_LOG_TYPE; i++) {
if (strncasecmp(str, failure_log_type_prefixes[i], len) == 0 &&
Expand Down Expand Up @@ -281,7 +282,7 @@ static void cmd_log_find(int argc, char *argv[])

static const char *t_cmd_log_error_trim(const char *orig)
{
unsigned int pos;
size_t pos;

/* Trim whitespace from suffix and remove ':' if it exists */
for (pos = strlen(orig); pos > 0; pos--) {
Expand Down
2 changes: 1 addition & 1 deletion src/doveadm/doveadm-mail-mailbox.c
Expand Up @@ -220,7 +220,7 @@ cmd_mailbox_create_run(struct doveadm_mail_cmd_context *_ctx,

array_foreach(&ctx->mailboxes, namep) {
const char *name = *namep;
unsigned int len;
size_t len;
bool directory = FALSE;

ns = mail_namespace_find(user->namespaces, name);
Expand Down
6 changes: 3 additions & 3 deletions src/doveadm/doveadm-mail.c
Expand Up @@ -699,7 +699,7 @@ doveadm_mail_cmd_try_find_multi_word(const struct doveadm_mail_cmd *cmd,
const char *cmdname, int *argc,
const char *const **argv)
{
unsigned int len;
size_t len;

if (*argc < 2)
return FALSE;
Expand Down Expand Up @@ -727,7 +727,7 @@ doveadm_mail_cmd_find_from_argv(const char *cmd_name, int *argc,
const char *const **argv)
{
const struct doveadm_mail_cmd *cmd;
unsigned int cmd_name_len;
size_t cmd_name_len;
const char *const *orig_argv;
int orig_argc;

Expand Down Expand Up @@ -822,7 +822,7 @@ void doveadm_mail_try_help_name(const char *cmd_name)
bool doveadm_mail_has_subcommands(const char *cmd_name)
{
const struct doveadm_mail_cmd *cmd;
unsigned int len = strlen(cmd_name);
size_t len = strlen(cmd_name);

array_foreach(&doveadm_mail_cmds, cmd) {
if (strncmp(cmd->name, cmd_name, len) == 0 &&
Expand Down
6 changes: 3 additions & 3 deletions src/doveadm/doveadm-print-table.c
Expand Up @@ -19,7 +19,7 @@ struct doveadm_print_table_header {
const char *key;
const char *title;
enum doveadm_print_header_flags flags;
unsigned int min_length, max_length, length;
size_t min_length, max_length, length;
};

struct doveadm_print_table_context {
Expand Down Expand Up @@ -51,8 +51,8 @@ static void doveadm_calc_header_length(void)
{
struct doveadm_print_table_header *headers;
const char *value, *const *values;
unsigned int i, line, len, hdr_count, value_count, line_count;
unsigned int max_length, orig_length, diff;
unsigned int i, line, hdr_count, value_count, line_count;
size_t len, max_length, orig_length, diff;

ctx->lengths_set = TRUE;

Expand Down

0 comments on commit 2ac5f36

Please sign in to comment.