Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix potentially overflow msg_namelen value in SupervisedSystemd #1891

Merged
merged 1 commit into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions src/cli/daemon_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,20 @@ inline bool SupervisedSystemd() {
return false;
}

sockaddr_un su;
memset(&su, 0, sizeof(su));
sockaddr_un su = {};
su.sun_family = AF_UNIX;
strncpy(su.sun_path, notify_socket, sizeof(su.sun_path) - 1);
su.sun_path[sizeof(su.sun_path) - 1] = '\0';
if (notify_socket[0] == '@') su.sun_path[0] = '\0';

iovec iov;
memset(&iov, 0, sizeof(iov));
iovec iov = {};
std::string ready = "READY=1";
iov.iov_base = &ready[0];
iov.iov_base = ready.data();
iov.iov_len = ready.size();

msghdr hdr;
memset(&hdr, 0, sizeof(hdr));
msghdr hdr = {};
hdr.msg_name = &su;
hdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(notify_socket);
hdr.msg_namelen = offsetof(sockaddr_un, sun_path) + strlen(su.sun_path);
hdr.msg_iov = &iov;
hdr.msg_iovlen = 1;

Expand Down
2 changes: 1 addition & 1 deletion src/common/cron.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Status Cron::SetScheduleTime(const std::vector<std::string> &args) {
return Status::OK();
}

bool Cron::IsTimeMatch(struct tm *tm) {
bool Cron::IsTimeMatch(tm *tm) {
if (tm->tm_min == last_tm_.tm_min && tm->tm_hour == last_tm_.tm_hour && tm->tm_mday == last_tm_.tm_mday &&
tm->tm_mon == last_tm_.tm_mon && tm->tm_wday == last_tm_.tm_wday) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/common/cron.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Cron {
~Cron() = default;

Status SetScheduleTime(const std::vector<std::string> &args);
bool IsTimeMatch(struct tm *tm);
bool IsTimeMatch(tm *tm);
std::string ToString() const;
bool IsEnabled() const;

Expand Down
6 changes: 3 additions & 3 deletions src/common/io_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ StatusOr<std::vector<std::string>> LookupHostByName(const std::string &host) {
for (auto p = servinfo; p != nullptr; p = p->ai_next) {
char ip[INET6_ADDRSTRLEN] = {};
if (p->ai_family == AF_INET) {
inet_ntop(p->ai_family, &((struct sockaddr_in *)p->ai_addr)->sin_addr, ip, sizeof(ip));
inet_ntop(p->ai_family, &((sockaddr_in *)p->ai_addr)->sin_addr, ip, sizeof(ip));
} else {
inet_ntop(p->ai_family, &((struct sockaddr_in6 *)p->ai_addr)->sin6_addr, ip, sizeof(ip));
inet_ntop(p->ai_family, &((sockaddr_in6 *)p->ai_addr)->sin6_addr, ip, sizeof(ip));
}
ips.emplace_back(ip);
}
Expand Down Expand Up @@ -350,7 +350,7 @@ StatusOr<std::tuple<std::string, uint32_t>> GetPeerAddr(int fd) {
int GetLocalPort(int fd) {
sockaddr_in6 address;
socklen_t len = sizeof(address);
if (getsockname(fd, (struct sockaddr *)&address, &len) == -1) {
if (getsockname(fd, (sockaddr *)&address, &len) == -1) {
return 0;
}

Expand Down
4 changes: 2 additions & 2 deletions src/server/redis_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void Connection::Close() {

void Connection::Detach() { owner_->DetachConnection(this); }

void Connection::OnRead(struct bufferevent *bev) {
void Connection::OnRead(bufferevent *bev) {
DLOG(INFO) << "[connection] on read: " << bufferevent_getfd(bev);

SetLastInteraction();
Expand All @@ -93,7 +93,7 @@ void Connection::OnRead(struct bufferevent *bev) {
}
}

void Connection::OnWrite(struct bufferevent *bev) {
void Connection::OnWrite(bufferevent *bev) {
if (IsFlagEnabled(kCloseAfterReply) || IsFlagEnabled(kCloseAsync)) {
Close();
}
Expand Down
4 changes: 2 additions & 2 deletions src/server/redis_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class Connection : public EvbufCallbackBase<Connection> {

void Close();
void Detach();
void OnRead(struct bufferevent *bev);
void OnWrite(struct bufferevent *bev);
void OnRead(bufferevent *bev);
void OnWrite(bufferevent *bev);
void OnEvent(bufferevent *bev, int16_t events);
void Reply(const std::string &msg);
void SendFile(int fd);
Expand Down
2 changes: 1 addition & 1 deletion src/server/worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ Status Worker::ListenUnixSocket(const std::string &path, int perm, int backlog)
return {Status::NotOK, evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR())};
}

if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
if (bind(fd, (sockaddr *)&sa, sizeof(sa)) < 0) {
return {Status::NotOK, evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR())};
}

Expand Down
2 changes: 1 addition & 1 deletion src/stats/stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Stats {
std::atomic<uint64_t> out_bytes = {0};

mutable std::shared_mutex inst_metrics_mutex;
std::vector<struct InstMetric> inst_metrics;
std::vector<InstMetric> inst_metrics;

std::atomic<uint64_t> fullsync_counter = {0};
std::atomic<uint64_t> psync_err_counter = {0};
Expand Down
12 changes: 6 additions & 6 deletions src/storage/storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -756,20 +756,20 @@ Status Storage::WriteToPropagateCF(const std::string &key, const std::string &va
}

Status Storage::ShiftReplId() {
const char *charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int charset_len = static_cast<int>(strlen(charset));
static constexpr std::string_view charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

// Do nothing if rsid psync is not enabled
if (!config_->use_rsid_psync) return Status::OK();

std::random_device rd;
std::mt19937 gen(rd() + getpid());
std::uniform_int_distribution<> distrib(0, charset_len - 1);
std::string rand_str;
std::uniform_int_distribution<size_t> distrib(0, charset.size() - 1);

std::string rand_str(kReplIdLength, 0);
for (int i = 0; i < kReplIdLength; i++) {
rand_str.push_back(charset[distrib(gen)]);
rand_str[i] = charset[distrib(gen)];
}
replid_ = rand_str;
replid_ = std::move(rand_str);
LOG(INFO) << "[replication] New replication id: " << replid_;

// Write new replication id into db engine
Expand Down