Skip to content

Commit f2f4131

Browse files
eslammedhatkawasaki
authored andcommitted
DRBD: replace strcpy with strscpy
strcpy is deprecated due to lack of bounds checking. This patch replaces strcpy with strscpy, the recommended alternative for null terminated strings, to follow best practices. I had to do a small refactor for __drbd_send_protocol since it uses strlen anyways. so why not use that for strscpy. V2: - I forgot about null termination so i fixed it. Signed-off-by: Eslam Khafagy <[email protected]>
1 parent 198825c commit f2f4131

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

drivers/block/drbd/drbd_main.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -742,9 +742,9 @@ int drbd_send_sync_param(struct drbd_peer_device *peer_device)
742742
}
743743

744744
if (apv >= 88)
745-
strcpy(p->verify_alg, nc->verify_alg);
745+
strscpy(p->verify_alg, nc->verify_alg);
746746
if (apv >= 89)
747-
strcpy(p->csums_alg, nc->csums_alg);
747+
strscpy(p->csums_alg, nc->csums_alg);
748748
rcu_read_unlock();
749749

750750
return drbd_send_command(peer_device, sock, cmd, size, NULL, 0);
@@ -771,10 +771,6 @@ int __drbd_send_protocol(struct drbd_connection *connection, enum drbd_packet cm
771771
return -EOPNOTSUPP;
772772
}
773773

774-
size = sizeof(*p);
775-
if (connection->agreed_pro_version >= 87)
776-
size += strlen(nc->integrity_alg) + 1;
777-
778774
p->protocol = cpu_to_be32(nc->wire_protocol);
779775
p->after_sb_0p = cpu_to_be32(nc->after_sb_0p);
780776
p->after_sb_1p = cpu_to_be32(nc->after_sb_1p);
@@ -787,8 +783,13 @@ int __drbd_send_protocol(struct drbd_connection *connection, enum drbd_packet cm
787783
cf |= CF_DRY_RUN;
788784
p->conn_flags = cpu_to_be32(cf);
789785

790-
if (connection->agreed_pro_version >= 87)
791-
strcpy(p->integrity_alg, nc->integrity_alg);
786+
size = sizeof(*p);
787+
if (connection->agreed_pro_version >= 87) {
788+
int integrity_len = strlen(nc->integrity_alg) + 1;
789+
size += integrity_len;
790+
strscpy(p->integrity_alg, nc->integrity_alg, integrity_len);
791+
}
792+
792793
rcu_read_unlock();
793794

794795
return __conn_send_command(connection, sock, cmd, size, NULL, 0);

drivers/block/drbd/drbd_receiver.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3985,14 +3985,14 @@ static int receive_SyncParam(struct drbd_connection *connection, struct packet_i
39853985
*new_net_conf = *old_net_conf;
39863986

39873987
if (verify_tfm) {
3988-
strcpy(new_net_conf->verify_alg, p->verify_alg);
3988+
strscpy(new_net_conf->verify_alg, p->verify_alg);
39893989
new_net_conf->verify_alg_len = strlen(p->verify_alg) + 1;
39903990
crypto_free_shash(peer_device->connection->verify_tfm);
39913991
peer_device->connection->verify_tfm = verify_tfm;
39923992
drbd_info(device, "using verify-alg: \"%s\"\n", p->verify_alg);
39933993
}
39943994
if (csums_tfm) {
3995-
strcpy(new_net_conf->csums_alg, p->csums_alg);
3995+
strscpy(new_net_conf->csums_alg, p->csums_alg);
39963996
new_net_conf->csums_alg_len = strlen(p->csums_alg) + 1;
39973997
crypto_free_shash(peer_device->connection->csums_tfm);
39983998
peer_device->connection->csums_tfm = csums_tfm;

0 commit comments

Comments
 (0)