Skip to content

Commit

Permalink
random: use ceph::util for non-cryptographic randomness
Browse files Browse the repository at this point in the history
Signed-off-by: Casey Bodley <[email protected]>
  • Loading branch information
cbodley committed Oct 9, 2017
1 parent 664c4f3 commit 6acfeef
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 93 deletions.
3 changes: 2 additions & 1 deletion src/auth/cephx/CephxClientHandler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "CephxProtocol.h"

#include "auth/KeyRing.h"
#include "include/random.h"
#include "common/config.h"
#include "common/dout.h"

Expand Down Expand Up @@ -53,7 +54,7 @@ int CephxClientHandler::build_request(bufferlist& bl) const
}

CephXAuthenticate req;
get_random_bytes((char *)&req.client_challenge, sizeof(req.client_challenge));
req.client_challenge = ceph::util::generate_random_number<uint64_t>();
std::string error;
cephx_calc_client_server_challenge(cct, secret, server_challenge,
req.client_challenge, &req.key, error);
Expand Down
7 changes: 4 additions & 3 deletions src/auth/cephx/CephxServiceHandler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <errno.h>
#include <sstream>

#include "include/random.h"
#include "common/config.h"
#include "common/debug.h"

Expand All @@ -30,9 +31,9 @@ int CephxServiceHandler::start_session(EntityName& name, bufferlist::iterator& i
{
entity_name = name;

get_random_bytes((char *)&server_challenge, sizeof(server_challenge));
if (!server_challenge)
server_challenge = 1; // always non-zero.
uint64_t min = 1; // always non-zero
uint64_t max = std::numeric_limits<uint64_t>::max();
server_challenge = ceph::util::generate_random_number<uint64_t>(min, max);
ldout(cct, 10) << "start_session server_challenge " << hex << server_challenge << dec << dendl;

CephXServerChallenge ch;
Expand Down
4 changes: 2 additions & 2 deletions src/ceph_mds.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ using namespace std;

#include "include/ceph_features.h"
#include "include/compat.h"
#include "include/random.h"

#include "common/config.h"
#include "common/strtol.h"
Expand Down Expand Up @@ -144,8 +145,7 @@ int main(int argc, const char **argv)
"MDS names may not start with a numeric digit." << dendl;
}

uint64_t nonce = 0;
get_random_bytes((char*)&nonce, sizeof(nonce));
auto nonce = ceph::util::generate_random_number<uint64_t>();

std::string public_msgr_type = g_conf->ms_public_type.empty() ? g_conf->get_val<std::string>("ms_type") : g_conf->ms_public_type;
Messenger *msgr = Messenger::create(g_ceph_context, public_msgr_type,
Expand Down
4 changes: 2 additions & 2 deletions src/mds/DamageTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#define DAMAGE_TABLE_H_

#include "mdstypes.h"
#include "auth/Crypto.h"
#include "include/random.h"

class CDir;

Expand All @@ -43,7 +43,7 @@ class DamageEntry

DamageEntry()
{
id = get_random(0, 0xffffffff);
id = ceph::util::generate_random_number<damage_entry_id_t>(0, 0xffffffff);
reported_at = ceph_clock_now();
}

Expand Down
3 changes: 1 addition & 2 deletions src/msg/Messenger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
Messenger *Messenger::create_client_messenger(CephContext *cct, string lname)
{
std::string public_msgr_type = cct->_conf->ms_public_type.empty() ? cct->_conf->get_val<std::string>("ms_type") : cct->_conf->ms_public_type;
uint64_t nonce = 0;
get_random_bytes((char*)&nonce, sizeof(nonce));
auto nonce = ceph::util::generate_random_number<uint64_t>();
return Messenger::create(cct, public_msgr_type, entity_name_t::CLIENT(),
std::move(lname), nonce, 0);
}
Expand Down
16 changes: 5 additions & 11 deletions src/msg/async/AsyncConnection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <unistd.h>

#include "include/Context.h"
#include "include/random.h"
#include "common/errno.h"
#include "AsyncMessenger.h"
#include "AsyncConnection.h"
Expand Down Expand Up @@ -2025,21 +2026,16 @@ void AsyncConnection::discard_out_queue()
out_q.clear();
}

int AsyncConnection::randomize_out_seq()
void AsyncConnection::randomize_out_seq()
{
if (get_features() & CEPH_FEATURE_MSG_AUTH) {
// Set out_seq to a random value, so CRC won't be predictable. Don't bother checking seq_error
// here. We'll check it on the call. PLR
uint64_t rand_seq;
int seq_error = get_random_bytes((char *)&rand_seq, sizeof(rand_seq));
rand_seq &= SEQ_MASK;
// Set out_seq to a random value, so CRC won't be predictable.
auto rand_seq = ceph::util::generate_random_number<uint64_t>(0, SEQ_MASK);
lsubdout(async_msgr->cct, ms, 10) << __func__ << " randomize_out_seq " << rand_seq << dendl;
out_seq = rand_seq;
return seq_error;
} else {
// previously, seq #'s always started at 0.
out_seq = 0;
return 0;
}
}

Expand Down Expand Up @@ -2137,9 +2133,7 @@ void AsyncConnection::was_session_reset()

dispatch_queue->queue_remote_reset(this);

if (randomize_out_seq()) {
ldout(async_msgr->cct, 15) << __func__ << " could not get random bytes to set seq number for session reset; set seq number to " << out_seq << dendl;
}
randomize_out_seq();

in_seq = 0;
connect_seq = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/msg/async/AsyncConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class AsyncConnection : public Connection {
void discard_out_queue();
void discard_requeued_up_to(uint64_t seq);
void requeue_sent();
int randomize_out_seq();
void randomize_out_seq();
void handle_ack(uint64_t seq);
void _append_keepalive_or_ack(bool ack=false, utime_t *t=NULL);
ssize_t write_message(Message *m, bufferlist& bl, bool more);
Expand Down
21 changes: 6 additions & 15 deletions src/msg/simple/Pipe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@

// Below included to get encode_encrypt(); That probably should be in Crypto.h, instead

#include "auth/Crypto.h"
#include "auth/cephx/CephxProtocol.h"
#include "auth/AuthSessionHandler.h"

#include "include/sock_compat.h"
#include "include/random.h"

// Constant to limit starting sequence number to 2^31. Nothing special about it, just a big number. PLR
#define SEQ_MASK 0x7fffffff
Expand Down Expand Up @@ -160,10 +160,7 @@ Pipe::Pipe(SimpleMessenger *r, int st, PipeConnection *con)
connection_state->pipe = get();
}

if (randomize_out_seq()) {
lsubdout(msgr->cct,ms,15) << "Pipe(): Could not get random bytes to set seq number for session reset; set seq number to " << out_seq << dendl;
}

randomize_out_seq();

msgr->timeout = msgr->cct->_conf->ms_tcp_read_timeout * 1000; //convert to ms
if (msgr->timeout == 0)
Expand Down Expand Up @@ -1546,19 +1543,15 @@ void Pipe::fault(bool onread)
}
}

int Pipe::randomize_out_seq()
void Pipe::randomize_out_seq()
{
if (connection_state->get_features() & CEPH_FEATURE_MSG_AUTH) {
// Set out_seq to a random value, so CRC won't be predictable. Don't bother checking seq_error
// here. We'll check it on the call. PLR
int seq_error = get_random_bytes((char *)&out_seq, sizeof(out_seq));
out_seq &= SEQ_MASK;
// Set out_seq to a random value, so CRC won't be predictable.
out_seq = ceph::util::generate_random_number<uint64_t>(0, SEQ_MASK);
lsubdout(msgr->cct, ms, 10) << "randomize_out_seq " << out_seq << dendl;
return seq_error;
} else {
// previously, seq #'s always started at 0.
out_seq = 0;
return 0;
}
}

Expand All @@ -1574,9 +1567,7 @@ void Pipe::was_session_reset()

msgr->dispatch_queue.queue_remote_reset(connection_state.get());

if (randomize_out_seq()) {
lsubdout(msgr->cct,ms,15) << "was_session_reset(): Could not get random bytes to set seq number for session reset; set seq number to " << out_seq << dendl;
}
randomize_out_seq();

in_seq = 0;
connect_seq = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/msg/simple/Pipe.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ static const int SM_IOV_MAX = (IOV_MAX >= 1024 ? IOV_MAX / 4 : IOV_MAX);
void writer();
void unlock_maybe_reap();

int randomize_out_seq();
void randomize_out_seq();

int read_message(Message **pm,
AuthSessionHandler *session_security_copy);
Expand Down
7 changes: 3 additions & 4 deletions src/rgw/rgw_data_sync.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#include "cls/lock/cls_lock_client.h"

#include "auth/Crypto.h"
#include "include/random.h"

#include <boost/asio/yield.hpp>

Expand Down Expand Up @@ -683,8 +683,7 @@ int RGWRemoteDataLog::init_sync_status(int num_shards)
}
RGWDataSyncEnv sync_env_local = sync_env;
sync_env_local.http_manager = &http_manager;
uint64_t instance_id;
get_random_bytes((char *)&instance_id, sizeof(instance_id));
auto instance_id = ceph::util::generate_random_number<uint64_t>();
ret = crs.run(new RGWInitDataSyncStatusCoroutine(&sync_env_local, num_shards, instance_id, tn, &sync_status));
http_manager.stop();
return ret;
Expand Down Expand Up @@ -1552,7 +1551,7 @@ class RGWDataSyncCR : public RGWCoroutine {
tn->log(20, SSTR("init"));
sync_status.sync_info.num_shards = num_shards;
uint64_t instance_id;
get_random_bytes((char *)&instance_id, sizeof(instance_id));
instance_id = ceph::util::generate_random_number<uint64_t>();
yield call(new RGWInitDataSyncStatusCoroutine(sync_env, num_shards, instance_id, tn, &sync_status));
if (retcode < 0) {
tn->log(0, SSTR("ERROR: failed to init sync, retcode=" << retcode));
Expand Down
9 changes: 3 additions & 6 deletions src/rgw/rgw_gc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "cls/rgw/cls_rgw_client.h"
#include "cls/refcount/cls_refcount_client.h"
#include "cls/lock/cls_lock_client.h"
#include "auth/Crypto.h"
#include "include/random.h"

#include <list>

Expand Down Expand Up @@ -240,14 +240,11 @@ int RGWGC::process()
{
int max_secs = cct->_conf->rgw_gc_processor_max_time;

unsigned start;
int ret = get_random_bytes((char *)&start, sizeof(start));
if (ret < 0)
return ret;
const int start = ceph::util::generate_random_number(0, max_objs - 1);

for (int i = 0; i < max_objs; i++) {
int index = (i + start) % max_objs;
ret = process(index, max_secs);
int ret = process(index, max_secs);
if (ret < 0)
return ret;
}
Expand Down
9 changes: 3 additions & 6 deletions src/rgw/rgw_lc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "common/Formatter.h"
#include <common/errno.h>
#include "auth/Crypto.h"
#include "include/random.h"
#include "cls/rgw/cls_rgw_client.h"
#include "cls/lock/cls_lock_client.h"
#include "rgw_common.h"
Expand Down Expand Up @@ -597,14 +597,11 @@ int RGWLC::process()
{
int max_secs = cct->_conf->rgw_lc_lock_max_time;

unsigned start;
int ret = get_random_bytes((char *)&start, sizeof(start));
if (ret < 0)
return ret;
const int start = ceph::util::generate_random_number(0, max_objs - 1);

for (int i = 0; i < max_objs; i++) {
int index = (i + start) % max_objs;
ret = process(index, max_secs);
int ret = process(index, max_secs);
if (ret < 0)
return ret;
}
Expand Down
23 changes: 6 additions & 17 deletions src/rgw/rgw_rados.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ using namespace librados;
#include <atomic>
#include <list>
#include <map>
#include "auth/Crypto.h" // get_random_bytes()
#include "include/random.h"

#include "rgw_log.h"

Expand Down Expand Up @@ -6002,24 +6002,13 @@ int RGWRados::select_legacy_bucket_placement(RGWZonePlacementInfo *rule_info)
}
}

map<string, bufferlist>::iterator miter;
auto miter = m.begin();
if (m.size() > 1) {
vector<string> v;
for (miter = m.begin(); miter != m.end(); ++miter) {
v.push_back(miter->first);
}

uint32_t r;
ret = get_random_bytes((char *)&r, sizeof(r));
if (ret < 0)
return ret;

int i = r % v.size();
pool_name = v[i];
} else {
miter = m.begin();
pool_name = miter->first;
// choose a pool at random
auto r = ceph::util::generate_random_number<size_t>(0, m.size() - 1);
std::advance(miter, r);
}
pool_name = miter->first;

rule_info->data_pool = pool_name;
rule_info->data_extra_pool = pool_name;
Expand Down
8 changes: 2 additions & 6 deletions src/rgw/rgw_swift_auth.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "common/ceph_crypto.h"
#include "common/Clock.h"

#include "auth/Crypto.h"
#include "include/random.h"

#include "rgw_client_io.h"
#include "rgw_http_client.h"
Expand Down Expand Up @@ -455,11 +455,7 @@ static int build_token(const string& swift_user,
static int encode_token(CephContext *cct, string& swift_user, string& key,
bufferlist& bl)
{
uint64_t nonce;

int ret = get_random_bytes((char *)&nonce, sizeof(nonce));
if (ret < 0)
return ret;
const auto nonce = ceph::util::generate_random_number<uint64_t>();

utime_t expiration = ceph_clock_now();
expiration += cct->_conf->rgw_swift_token_expiration;
Expand Down
Loading

0 comments on commit 6acfeef

Please sign in to comment.