cld: fix CLD_INODE_NAME_MAX woes
PATCH v2 1/2 cld: fix CLD_INODE_NAME_MAX woes
v2: one part of this patch was originally accidentally mixed into patch 2
When we create a static buffer for an inode name, and treat it like a null-terminated string, it needs to be of length CLD_INODE_NAME_MAX + 1 so that it can hold the NULL-terminator.
In cldc_del and cldc_open, we should check that the user-submitted inode name is less than or equal to CLD_INODE_NAME_MAX. Formerly we were just checking that it wasn't too big to fit in the packet.
When copying the inode name out of struct cld_dirent_cur, use snprintf rather than strcpy to ensure that we never overflow the buffer. This isn't strictly necessary if all other checks are working perfectly, but it seems prudent.
--- include/cldc.h | 2 +- lib/cldc.c | 4 ++-- tools/cldcli.c | 1 + 3 files changed, 4 insertions(+), 3 deletions(-)
include/cldc.h +1 -1
@@ -41,7 +41,7 @@ struct cldc_call_opts {struct cld_msg_get_resp resp;const char *buf;unsigned int size; -char inode_name[CLD_INODE_NAME_MAX ]; +char inode_name[CLD_INODE_NAME_MAX +1 ]; } get; } u; };
lib/cldc.c +2 -2
@@ -903,7 +903,7 @@ int cldc_del(struct cldc_session *sess, const struct cldc_call_opts *copts,return -EINVAL ; plen =strlen (pathname); -if (plen >65530 ) +if (plen >CLD_INODE_NAME_MAX )return -EINVAL ;/* create DEL message */
@@ -974,7 +974,7 @@ int cldc_open(struct cldc_session *sess,return -EINVAL ; plen =strlen (pathname); -if (plen >65530 ) +if (plen >CLD_INODE_NAME_MAX )return -EINVAL ;/* create OPEN message */
tools/cldcli.c +1 -0
@@ -254,6 +254,7 @@ static int cb_ls_2(struct cldc_call_opts *copts_in, enum cle_err_codes errc) s =cldc_dirent_name (&dc);strcpy (lsr.name, s); +snprintf (lsr.name,CLD_INODE_NAME_MAX +1 , s);free (s);write_from_thread (&lsr,sizeof (lsr));
Signature · 1 line
1.6.2.5
PATCH v2 2/2 cld: kill CLD_MAX_PKT_MSG, add CLD_MAX_PAYLOAD_SZ
Get rid of CLD_MAX_PKT_MSG. It only existed so that we could use static arrays in a few places.
Create CLD_MAX_PAYLOAD_SZ to represent the maximum size of a message that the API user can GET or PUT. Reducing this constant could break users who relied on the old maximum data size, so we should try not to do it often.
--- include/cld_msg.h | 7 ++++--- include/cldc.h | 4 ++-- lib/cldc.c | 24 +++++++++++++++++------- server/msg.c | 7 +++++++ server/session.c | 12 ++++++++---- 5 files changed, 38 insertions(+), 16 deletions(-)
include/cld_msg.h +4 -3
@@ -34,9 +34,10 @@ enum {CLD_MAX_SECRET_KEY =128 ,/**< includes req. nul */ CLD_MAX_PKT_MSG_SZ =1024 , -CLD_MAX_PKT_MSG =128 , - CLD_MAX_MSG_SZ = CLD_MAX_PKT_MSG * 1024, /**< maximum total - msg size, including all packets */ +CLD_MAX_PAYLOAD_SZ =131072 ,/**< maximum size of data that users +can GET or PUT */ + CLD_MAX_MSG_SZ = 196608, /**< maximum total + msg size, including all packets */ };/** available RPC operations */
include/cldc.h +2 -2
@@ -73,10 +73,10 @@ struct cldc_msg {int data_len;int n_pkts; -struct cldc_pkt_info *pkt_info[CLD_MAX_PKT_MSG ]; +uint8_t *data;/* must be at end of struct */ -uint8_t data[0 ]; +struct cldc_pkt_info *pkt_info[0 ]; };/** an open file handle associated with a session */
lib/cldc.c +17 -7
@@ -260,7 +260,9 @@ static void cldc_msg_free(struct cldc_msg *msg)if (!msg)return ; -for (i =0 ; i <CLD_MAX_PKT_MSG ; i++) +free (msg->data); + +for (i =0 ; i < msg->n_pkts; i++)free (msg->pkt_info[i]);free (msg);
@@ -492,14 +494,23 @@ static struct cldc_msg *cldc_new_msg(struct cldc_session *sess,struct cldc_msg *msg;struct cld_msg_hdr *hdr;struct timeval tv; -int i, data_left; +int n_pkts, i, data_left;void *p;gettimeofday (&tv,NULL ); - msg =calloc (1 ,sizeof (*msg) + msg_len); + n_pkts = msg_len /CLD_MAX_PKT_MSG_SZ ; + n_pkts += ((msg_len %CLD_MAX_PKT_MSG_SZ ) ?1 :0 ); + + msg =calloc (1 ,sizeof (*msg) + + n_pkts *sizeof (struct cldc_pkt_info *));if (!msg)return NULL ; + msg->data =calloc (1 , msg_len); +if (!msg->data) { +free (msg); +return NULL ; + }__cld_rand64 (&msg->xid);
@@ -512,8 +523,7 @@ static struct cldc_msg *cldc_new_msg(struct cldc_session *sess, msg->data_len = msg_len; - msg->n_pkts = msg_len /CLD_MAX_PKT_MSG_SZ ; - msg->n_pkts += ((msg_len %CLD_MAX_PKT_MSG_SZ ) ?1 :0 ); + msg->n_pkts = n_pkts; p = msg->data; data_left = msg_len;
@@ -542,7 +552,7 @@ static struct cldc_msg *cldc_new_msg(struct cldc_session *sess, data_left -= pkt_len; } - hdr = (struct cld_msg_hdr *) &msg->data[0 ]; + hdr = (struct cld_msg_hdr *) msg->data;memcpy (&hdr->magic,CLD_MSG_MAGIC ,CLD_MAGIC_SZ ); hdr->op = op; hdr->xid = msg->xid;
@@ -1099,7 +1109,7 @@ int cldc_put(struct cldc_fh *fh, const struct cldc_call_opts *copts,struct cldc_msg *msg;struct cld_msg_put *put; -if (!data || !data_len || data_len >CLD_MAX_MSG_SZ ) +if (!data || !data_len || data_len >CLD_MAX_PAYLOAD_SZ )return -EINVAL ;if (!fh->valid)
server/msg.c +7 -0
@@ -757,6 +757,13 @@ void msg_put(struct msg_params *mp)/* make sure additional input data as large as expected */ data_size =le32_to_cpu (msg->data_size); +if (data_size >CLD_MAX_PAYLOAD_SZ ) { +HAIL_ERR (&srv_log,"%s: can't PUT %d bytes of data: " +"%d is the maximum.\n" , + __func__, data_size,CLD_MAX_PAYLOAD_SZ ); + resp_rc =CLE_BAD_PKT ; + goto err_out_noabort; + }if (mp->msg_len != (data_size +sizeof (*msg))) { HAIL_INFO(&srv_log, "PUT len mismatch: msg len %zu, " "wanted %zu + %u (== %zu)",
server/session.c +8 -4
@@ -623,13 +623,20 @@ bool sess_sendmsg(struct session *sess, const void *msg_, size_t msglen,struct cld_packet *outpkt;unsigned int n_pkts, i;size_t pkt_len, msg_left = msglen; -struct session_outpkt *pkts[CLD_MAX_PKT_MSG ], *op; +struct session_outpkt **pkts, *op;GList *tmp_root =NULL ;const void *p;bool first_frag = true; +if (msglen >CLD_MAX_MSG_SZ ) { +HAIL_ERR (&srv_log,"%s: message too big at %d bytes\n" , + __func__, msglen); +return false; + } + n_pkts = (msglen /CLD_MAX_PKT_MSG_SZ ); n_pkts += (msglen %CLD_MAX_PKT_MSG_SZ ) ?1 :0 ; + pkts =alloca (sizeof (struct session_outpkt *) * n_pkts);if (srv_log.verbose) {const struct cld_msg_hdr *hdr = msg_;
@@ -671,9 +678,6 @@ bool sess_sendmsg(struct session *sess, const void *msg_, size_t msglen, } } -if (n_pkts >CLD_MAX_PKT_MSG ) -return false; -/* pass 1: perform allocations */ for (i =0 ; i < n_pkts; i++) { pkts[i] = op = op_alloc(sizeof(*outpkt) +
Signature · 1 line
1.6.2.5
Jeff Garzik
Applied[PATCH] Re: [PATCHv2 2/2] cld: kill CLD_MAX_PKT_MSG, add CLD_MAX_PAYLOAD_SZ
Quoted 7 lines · Colin McCabe
Get rid of
CLD_MAX_PKT_MSG. It only existed so that we could use static arrays in a few places.Create
CLD_MAX_PAYLOAD_SZto represent the maximum size of a message that the API user can GET or PUT. Reducing this constant could break users who relied on the old maximum data size, so we should try not to do it often.
Quoted 8 lines
Signed-off-by Colin McCabe<[email protected]>--- include/cld_msg.h | 7 ++++--- include/cldc.h | 4 ++-- lib/cldc.c | 24 +++++++++++++++++------- server/msg.c | 7 +++++++ server/session.c | 12 ++++++++---- 5 files changed, 38 insertions(+), 16 deletions(-)
applied
I've attached the latest cld->cld.rpcgen working diff, current as of commit
commit a592ee77012f3c32d775c1e67fff66c944e7b5fe
Author: Colin McCabe <[email protected]> Date: Wed Feb 3 18:17:24 2010 -0500
libcldc: use typed message completion callback
.gitignore +4 -0
@@ -32,5 +32,9 @@ cld-config.h* cscope.* ncscope.* +# XDR files +*_rpc.h +*_rpc_xdr.c + # ignore Doxygen output directory gendoc
include/cldc.h +14 -18
@@ -22,7 +22,7 @@#include <sys/types.h> #include <stdbool.h> #include <glib.h> -#include <cld_msg.h> +#include <cld_msg_rpc.h> #include <cld_common.h> #include <hail_log.h>
@@ -35,34 +35,30 @@ struct cldc_call_opts {void *private;/* private; lib-owned */ -enum cld_msg_ops op; -union { -struct { -struct cld_msg_get_resp resp; -const char *buf; -unsigned int size; -char inode_name[CLD_INODE_NAME_MAX +1 ]; - } get; - } u; +struct cld_msg_get_resp resp; }; +void cldc_call_opts_get_data (struct cldc_call_opts *copts, +char **data,size_t *data_len); +struct cldc_pkt_info {int pkt_len; +int hdr_len;int retries; +char user[CLD_MAX_USERNAME ];/* must be at end of struct */ -struct cld_packet pkt; -uint8_t data[0 ]; +char data[0 ]; };/** an outgoing message, from client to server */ struct cldc_msg {uint64_t xid; - +enum cld_msg_op op;struct cldc_session *sess;ssize_t (*cb)(struct cldc_msg *,const void *,size_t , -enum cle_err_codes ); +enum cle_err_codes resp_rc);void *cb_private;struct cldc_call_opts copts;
@@ -71,18 +67,15 @@ struct cldc_msg {time_t expire_time; -int data_len;int n_pkts; -uint8_t *data; -/* must be at end of struct */ struct cldc_pkt_info *pkt_info[0 ]; };/** an open file handle associated with a session */ struct cldc_fh { -uint64_t fh_le;/* fh id, LE */ +uint64_t fh;struct cldc_session *sess;bool valid; };
@@ -129,8 +122,11 @@ struct cldc_session {bool confirmed; +enum cld_msg_op msg_buf_op;unsigned int msg_buf_len;char msg_buf[CLD_MAX_MSG_SZ ]; +char payload[CLD_MAX_PAYLOAD_SZ ]; +char inode_name_temp[CLD_INODE_NAME_MAX ]; };/** Information for a single CLD server host */
include/cld_common.h +2 -2
@@ -3,7 +3,6 @@/* * Copyright 2009 Red Hat, Inc. -* Copyright 2010, Colin McCabe * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by
@@ -24,6 +23,7 @@#include <stdbool.h> #include <string.h> #include <time.h> +#include <cld_msg_rpc.h> #define CLD_ALIGN8 (n) ((8 - ((n) & 7)) & 7)
@@ -91,7 +91,7 @@ extern int __cld_authsign(struct hail_lo const void *buf, size_t buf_len, void *sha);/* Returns a constant string representing a message operation */ -extern const char *__cld_opstr (enum cld_msg_ops op); +extern const char *__cld_opstr (enum cld_msg_op );/* * We use a unified format for sid so it can be searched in log files (* in vi).
include/cld_msg.h +0 -245
245 lines not on this page · raw message
include/cld_pkt.h +56 -0
56 lines not on this page · raw message
include/Makefile.am +1 -1
@@ -1,5 +1,5 @@ EXTRA_DIST = cld-private.h -include_HEADERS = cldc.h hail_log.h cld_common.h cld_msg.h +include_HEADERS = cldc.h hail_log.h cld_common.h cld_pkt.h
lib/cldc.c +347 -380
1,157 lines not on this page · raw message
Binary files cld/lib/cldc-dns.o and cld.rpcgen/lib/cldc-dns.o differ Binary files cld/lib/cldc.o and cld.rpcgen/lib/cldc.o differ Binary files cld/lib/cldc-udp.o and cld.rpcgen/lib/cldc-udp.o differ
lib/cld_msg_rpc.x +218 -0
218 lines not on this page · raw message
Binary files cld/lib/cld_msg_rpc_xdr.o and cld.rpcgen/lib/cld_msg_rpc_xdr.o differ
lib/common.c +5 -1
@@ -26,7 +26,11 @@#include <errno.h> #include <glib.h> #include <cld-private.h> -#include "cld_msg.h" +#include <openssl/sha.h> +#include <openssl/hmac.h> +#include "cld_msg_rpc.h" +#include <hail_log.h> +#include <syslog.h> /* duplicated from tools/cldcli.c; put in common header somewhere? */ #define ARRAY_SIZE (arr) (sizeof(arr) / sizeof((arr)[0]))
Binary files cld/lib/common.o and cld.rpcgen/lib/common.o differ Binary files cld/lib/libcldc.a and cld.rpcgen/lib/libcldc.a differ Binary files cld/lib/.libs/libcldc.a and cld.rpcgen/lib/.libs/libcldc.a differ Binary files cld/lib/libtimer.a and cld.rpcgen/lib/libtimer.a differ Binary files cld/lib/libtimer.o and cld.rpcgen/lib/libtimer.o differ
lib/Makefile.am +14 -1
@@ -1,10 +1,21 @@ +BUILT_SOURCES = cld_msg_rpc.h + EXTRA_DIST = libcldc.pc.in libcldc-uninstalled.pc.in INCLUDES = -I$(top_srcdir)/include \ @GLIB_CFLAGS@ +mostlyclean-local: + rm -f *_rpc.h *_rpc_xdr.c + +%_rpc.h: %_rpc.x + rpcgen -h $< > $@ + +%_rpc_xdr.c: %_rpc.x + rpcgen -c $< > $@ + LINK = $(LIBTOOL) --mode=link $(CC) $(CFLAGS) $(LDFLAGS) -o $@ lib_LTLIBRARIES = libcldc.la
@@ -15,7 +26,8 @@ libcldc_la_SOURCES = \ cldc-dns.c \ common.c \ libtimer.c \ - pkt.c + pkt.c \ + cld_msg_rpc_xdr.c libcldc_la_LDFLAGS = \ -version-info $(LIBCLDC_CURRENT):$(LIBCLDC_REVISION):$(LIBCLDC_AGE) \
@@ -25,3 +37,4 @@ libcldc_la_LDFLAGS = \ pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = libcldc.pc +include_HEADERS = cld_msg_rpc.h
lib/pkt.c +100 -2
121 lines not on this page · raw message
Binary files cld/lib/pkt.o and cld.rpcgen/lib/pkt.o differ Binary files cld/server/cld and cld.rpcgen/server/cld differ Binary files cld/server/cldbadm and cld.rpcgen/server/cldbadm differ Binary files cld/server/cldbadm.o and cld.rpcgen/server/cldbadm.o differ
server/cldb.h +1 -1
@@ -23,7 +23,7 @@#include <stdbool.h> #include <db.h> #include <cld-private.h> -#include <cld_msg.h> +#include <cld_msg_rpc.h> typedef uint64_t cldino_t ;
Binary files cld/server/cldb.o and cld.rpcgen/server/cldb.o differ
server/cld.h +66 -29
134 lines not on this page · raw message
Binary files cld/server/cld_msg_rpc_xdr.o and cld.rpcgen/server/cld_msg_rpc_xdr.o differ Binary files cld/server/common.o and cld.rpcgen/server/common.o differ Binary files cld/server/libtimer.o and cld.rpcgen/server/libtimer.o differ
server/Makefile.am +3 -1
@@ -1,5 +1,6 @@ INCLUDES = -I$(top_srcdir)/include \ + -I$(top_srcdir)/lib \ @GLIB_CFLAGS@ \ -DCLD_LIBDIR=\""$(libdir)"\" \ -DCLD_LOCAL_STATE_DIR="\"$(localstatedir)\""
@@ -8,7 +9,8 @@ sbin_PROGRAMS = cld cldbadm cld_SOURCES = cldb.h cld.h \ ../lib/common.c ../lib/libtimer.c ../lib/pkt.c \ - cldb.c msg.c server.c session.c util.c + cldb.c msg.c server.c session.c util.c \ + ../lib/cld_msg_rpc_xdr.c cld_LDADD = @CRYPTO_LIBS@ @GLIB_LIBS@ @DB4_LIBS@ cldbadm_SOURCES = cldb.h cldbadm.c
server/msg.c +102 -195
604 lines not on this page · raw message
Binary files cld/server/msg.o and cld.rpcgen/server/msg.o differ Binary files cld/server/pkt.o and cld.rpcgen/server/pkt.o differ
server/server.c +418 -283
278 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Binary files cld/server/server.o and cld.rpcgen/server/server.o differ
server/session.c +186 -223
409 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Binary files cld/server/session.o and cld.rpcgen/server/session.o differ Binary files cld/server/util.o and cld.rpcgen/server/util.o differ Binary files cld/test/it-works and cld.rpcgen/test/it-works differ Binary files cld/test/it-works.o and cld.rpcgen/test/it-works.o differ Binary files cld/test/libtest.a and cld.rpcgen/test/libtest.a differ Binary files cld/test/load-file-event and cld.rpcgen/test/load-file-event differ
The rest of this diff is not shown. View the raw message.
Binary files cld/test/load-file-event.o and cld.rpcgen/test/load-file-event.o differ Binary files cld/test/lock-file-event and cld.rpcgen/test/lock-file-event differ Binary files cld/test/lock-file-event.o and cld.rpcgen/test/lock-file-event.o differ
The rest of this diff is not shown. View the raw message.
Binary files cld/test/save-file-event and cld.rpcgen/test/save-file-event differ Binary files cld/test/save-file-event.o and cld.rpcgen/test/save-file-event.o differ Binary files cld/test/util.o and cld.rpcgen/test/util.o differ
Binary files cld/tools/cldcli and cld.rpcgen/tools/cldcli differ
The rest of this diff is not shown. View the raw message.
Binary files cld/tools/cldcli.o and cld.rpcgen/tools/cldcli.o differ
The rest of this diff is not shown. View the raw message.
Part of the diff is not shown here. View the raw message.
Jeff Garzik
AppliedQuoted 8 lines · Colin McCabe
When we create a static buffer for an inode name, and treat it like a null-terminated string, it needs to be of length
CLD_INODE_NAME_MAX+ 1 so that it can hold the NULL-terminator.In
cldc_delandcldc_open, we should check that the user-submitted inode name is less than or equal toCLD_INODE_NAME_MAX. Formerly we were just checking that it wasn't too big to fit in the packet.
Quoted 5 lines
When copying the inode name out of struct
cld_dirent_cur, use snprintf rather than strcpy to ensure that we never overflow the buffer. This isn't strictly necessary if all other checks are working perfectly, but it seems prudent.Signed-off-by Colin McCabe<[email protected]>
applied, after s/snprintf/strncpy/
In general, too, you should never pass a variable string into snprintf, as that may make a program vulnerable to printf format string attacks (user supplies "%s" as a username, for example).
A few other changes made to your XDR work:
* "\n" removed from log messages, as that is appended as needed by log implementation
* user_key() restored. that is our authentication hook, and it must be called, even though it merely returns the username passed to it at present.
* msg type renamed back to msg op
Jeff
Quoted 18 lines · Jeff Garzik
Quoted 17 lines · Colin McCabe
When we create a static buffer for an inode name, and treat it like a null-terminated string, it needs to be of length
CLD_INODE_NAME_MAX+ 1 so that it can hold the NULL-terminator.In
cldc_delandcldc_open, we should check that the user-submitted inode name
is less than or equal toCLD_INODE_NAME_MAX. Formerly we were just checking
that it wasn't too big to fit in the packet.When copying the inode name out of struct
cld_dirent_cur, use snprintf rather
than strcpy to ensure that we never overflow the buffer. This isn't strictly
necessary if all other checks are working perfectly, but it seems prudent.
Quoted 7 lines
Quoted 1 line
Signed-off-by Colin McCabe<[email protected]>applied, after s/snprintf/strncpy/
In general, too, you should never pass a variable string into snprintf, as that may make a program vulnerable to printf format string attacks (user supplies "%s" as a username, for example).
Oh, how embarassing. I hate it when people do that, now I did it too.
I tend to avoid strncpy because of how it always writes n bytes. If only glibc had picked up strlcpy.
Colin