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

INTERNAL: do_lqdetect_write method simple #790

Merged
merged 1 commit into from
Oct 31, 2024

Conversation

cheesecrust
Copy link

@cheesecrust cheesecrust commented Oct 23, 2024

๐Ÿ”— Related Issue

โŒจ๏ธ What I did

  • key ๋ฅผ ๋งŒ๋“œ๋Š” ๋กœ์ง๊ณผ ํ‚ค๋ฅผ ์ด์–ด๋ถ™์ด๋Š” ๋ถ€๋ถ„์„ ๊ฐ„์†Œํ™” ์‹œ์ผฐ์Šต๋‹ˆ๋‹ค.
  • https://github.com/jam2in/arcus-works/issues/627
    ์œ„ ์ด์Šˆ์— ๋งž์ถ”์–ด ์ž…๋ ฅ ๊ฐ€๋Šฅํ•œ ๋ฒ„ํผ์˜ ๊ณต๊ฐ„ ๋ณด๋‹ค ํฐ ์ž…๋ ฅ์ด ๋“ค์–ด์™”์„๋•Œ์— ์ฒ˜๋ฆฌ๋ฅผ ํฌํ•จํ•˜์˜€์Šต๋‹ˆ๋‹ค.
    • key๋ฅผ ์ž…๋ ฅํ•˜๊ธฐ ์ „์— offset ์ด length ๋ฅผ ๋„˜์„ ๊ฒฝ์šฐ ์ดํ›„์— snprintf ์— 0์„ ๋„˜๊ฒจ ์จ์ง€์ง€ ๋ชปํ•˜๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
    • ๋งˆ์ง€๋ง‰ ๊นŒ์ง€ ๋‹ค ์ป์„๋•Œ offset์ด ์ „์ฒด ์ฃผ์–ด์ง„ ๊ธธ์ด๋ณด๋‹ค ํฌ๋ฉด offset์„ length - 1 ๋กœ ์˜ฎ๊ธฐ๊ณ  ํ•ด๋‹น ์ž๋ฆฌ์— ์ค„๋ฐ”๊ฟˆ \n์„ ์‚ฝ์ž…ํ•˜์—ฌ ๋‹ค์Œ ๋ช…๋ น์„ ๋ฐ›์„ ์ค€๋น„ ํ•ฉ๋‹ˆ๋‹ค.

@cheesecrust cheesecrust marked this pull request as draft October 23, 2024 05:33
@namsic namsic self-requested a review October 23, 2024 09:48
Copy link
Collaborator

@jhpark816 jhpark816 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ฆฌ๋ทฐ ์™„๋ฃŒ

lqdetect.c Outdated Show resolved Hide resolved
@cheesecrust cheesecrust force-pushed the internal/do_lqdetect_write branch from bd7f5f0 to 75eead7 Compare October 28, 2024 01:23
@cheesecrust cheesecrust marked this pull request as ready for review October 28, 2024 01:49
Copy link
Collaborator

@jhpark816 jhpark816 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ฆฌ๋ทฐ ์™„๋ฃŒ

lqdetect.c Outdated Show resolved Hide resolved
lqdetect.c Outdated Show resolved Hide resolved
lqdetect.c Show resolved Hide resolved
@cheesecrust cheesecrust force-pushed the internal/do_lqdetect_write branch from 75eead7 to 6134867 Compare October 28, 2024 08:33
lqdetect.c Outdated Show resolved Hide resolved
@jhpark816
Copy link
Collaborator

@cheesecrust rebaseํ•ด ์ฃผ์„ธ์š”.

@cheesecrust cheesecrust force-pushed the internal/do_lqdetect_write branch 2 times, most recently from f0f3686 to 95c243f Compare October 29, 2024 04:44
@cheesecrust cheesecrust marked this pull request as draft October 29, 2024 04:48
lqdetect.c Outdated
snprintf(bufptr, length, "%s %s\n", keyptr, arg->query);
nwrite += strlen(bufptr);
buffer->offset += nwrite;
buffer->offset += snprintf(buffer->data + buffer->offset, length - buffer->offset, "%s %s\n", keyptr, arg->query);
Copy link
Collaborator

@jhpark816 jhpark816 Oct 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์•„๋ž˜์™€ ๊ฐ™์ด ํ•˜์‹œ์ฃ .

  • length ๊ฐ’ ์กฐ์ •
  • indentation์€ ์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด
    gettimeofday(&val, NULL);
    ptm = localtime(&val.tv_sec);
    length = ((nsaved+1) * LQ_INPUT_SIZE);

    buffer->offset += snprintf(buffer->data + buffer->offset, length - buffer->offset,
                               "%02d:%02d:%02d.%06ld %s <%u> %s ",
                               ptm->tm_hour, ptm->tm_min, ptm->tm_sec, (long)val.tv_usec,
                               client_ip, arg->overhead, command_str[cmd]);
    buffer->keypos[nsaved] = buffer->offset;
    buffer->keylen[nsaved] = keylen;

    buffer->offset += snprintf(buffer->data + buffer->offset, length - buffer->offset,
                               "%s %s\n", keyptr, arg->query);
    lqdetect.arg[cmd][nsaved] = *arg;
    buffer->nsaved += 1;

@cheesecrust cheesecrust force-pushed the internal/do_lqdetect_write branch 4 times, most recently from c573f16 to b2893d3 Compare October 30, 2024 02:14
@cheesecrust cheesecrust marked this pull request as ready for review October 30, 2024 02:21
@cheesecrust cheesecrust marked this pull request as draft October 30, 2024 02:56
@cheesecrust cheesecrust force-pushed the internal/do_lqdetect_write branch 3 times, most recently from 3f4b5dc to 2b755cb Compare October 30, 2024 06:11
@cheesecrust cheesecrust reopened this Oct 30, 2024
@jhpark816

This comment was marked as off-topic.

@uhm0311

This comment was marked as off-topic.

@jhpark816

This comment was marked as off-topic.

@uhm0311

This comment was marked as off-topic.

@namsic

This comment was marked as off-topic.

lqdetect.c Outdated
@@ -103,7 +103,7 @@ static bool is_command_duplicated(char *key, int keylen, enum lq_detect_command
case LQCMD_MOP_GET:
case LQCMD_SOP_GET:
for (int ii = 0; ii < count; ii++) {
if (strcmp(lqdetect.arg[cmd][ii].query, arg->query) == 0) {
if (buf->keylen[ii] > 0 && strcmp(lqdetect.arg[cmd][ii].query, arg->query) == 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keylen > 0 ํ™•์ธํ•˜๋Š” ์ด์œ ๊ฐ€ ๋ฌด์—‡์ธ๊ฐ€์š”?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • ๋กœ๊ทธ๊ฐ€ ๋‹ค ์•ˆ ์จ์กŒ์„ ๊ฒฝ์šฐ(ํ‚ค, ์ฟผ๋ฆฌ ๋ชจ๋‘ ํฌํ•จ)์— ํ•ด๋‹น ์ˆœ์„œ์˜ keylen์„ -1 ๋กœ ์ดˆ๊ธฐํ™” ์‹œ์ผœ ๋„˜์ณค๋‹ค๋Š” ๊ฒƒ์„ ํ‘œํ˜„ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์™„์ „ํ•˜์ง€ ๋ชปํ•œ ๋ผ์ธ์— ๋Œ€ํ•ด์„œ๋Š” ๋น„๊ต๋ฅผ ํ•ด์„œ๋Š” ์•ˆ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์—ฌ ์™„๋ฒฝํ•˜์ง€ ๋ชปํ•œ ๋ผ์ธ์„ ๋„˜์–ด๊ฐ€๊ธฐ ์œ„ํ•ด ์ถ”๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator

@namsic namsic Oct 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

snprintf์—์„œ overflow ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์—ฌ๊ธฐ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ lqdetect show ๋ช…๋ น ๋“ฑ์—์„œ๋„ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์•„๋ž˜ ํ•ญ๋ชฉ์„ ์ถฉ๋ถ„ํžˆ ํ™•์ธํ•˜๊ณ  ๊ณต์œ ํ•ด ์ฃผ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.
์ด์™€ ๊ด€๋ จํ•œ ์ˆ˜์ •์€ ๋‹ค๋ฅธ PR๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ๋‚˜์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

AS-IS

  • lqdetect_write ๋„์ค‘ snprintf์—์„œ overflow ๋ฐœ์ƒํ•  ๊ฒฝ์šฐ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„๋“ค
  • ๊ธฐ์กด ์ฝ”๋“œ ๊ธฐ์ค€ overflow ๋ฐœ์ƒํ•˜๋ฉด ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š”์ง€?

TO-BE

  • ๊ตฌํ˜„์„ ์–ด๋–ป๊ฒŒ ์ˆ˜์ •ํ•ด์•ผ ํ•˜๋Š”์ง€ (์—ฌ๋Ÿฌ ๊ตฌํ˜„ ๋ฐฉ์‹์ด ์žˆ์„ํ…๋ฐ, ๊ทธ ์ค‘ ๋‚ซ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š” ๋ฐฉ์‹๊ณผ ๊ทธ ์ด์œ )
  • ์ˆ˜์ •ํ•˜๋ฉด, ์ด์ „์— ๋ฌธ์ œ ๋ฐœ์ƒํ–ˆ๋˜ ์ƒํ™ฉ์—์„œ ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๊ฒŒ ๋˜๋Š”์ง€

@jhpark816

This comment was marked as off-topic.

@cheesecrust cheesecrust marked this pull request as ready for review October 30, 2024 09:01
@jhpark816
Copy link
Collaborator

์•„๋ž˜์™€ ๊ฐ™์ด ํ•ฉ์‹œ๋‹ค.

static void do_lqdetect_write(char *client_ip, char *key,
                              enum lq_detect_command cmd, struct lq_detect_argument *arg)
{
    struct tm *ptm;
    struct timeval val;
    struct lq_detect_buffer *buffer = &lqdetect.buffer[cmd];
    uint32_t nsaved = buffer->nsaved;
    uint32_t length, keylen = strlen(key);
    char keybuf[251];
    char *keyptr = key;

    if (keylen > 250) { /* long key string */
        keylen = snprintf(keybuf, sizeof(keybuf), "%.*s...%.*s",
                          124, key, 123, (key+keylen-123));
        keyptr = keybuf;
    }

    if (is_command_duplicated(keyptr, keylen, cmd, arg) == true) {
        return;
    }

    gettimeofday(&val, NULL);
    ptm = localtime(&val.tv_sec);

    length = ((nsaved+1) * LQ_INPUT_SIZE);
    snprintf(buffer->data + buffer->offset, length - buffer->offset,
             "%02d:%02d:%02d.%06ld %s <%u> %s ",
             ptm->tm_hour, ptm->tm_min, ptm->tm_sec, (long)val.tv_usec,
             client_ip, arg->overhead, command_str[cmd]);
    buffer->offset += strlen(buffer->data + buffer->offset);
    buffer->keypos[nsaved] = buffer->offset;
    buffer->keylen[nsaved] = keylen;

    snprintf(buffer->data + buffer->offset, length - buffer->offset,
             "%s %s\n", keyptr, arg->query);
    buffer->offset += strlen(buffer->data + buffer->offset);
    lqdetect.arg[cmd][nsaved] = *arg;
    buffer->nsaved += 1;
}

@cheesecrust
Copy link
Author

cheesecrust commented Oct 31, 2024

@jhpark816
snprintf ์‹œ overflow๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด '\n'์„ ์‚ฝ์ž…ํ•ด์ฃผ๋Š” ์ฝ”๋“œ๋„ ํ•„์š”ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

snprintf(buffer->data + buffer->offset, length - buffer->offset,
             "%s %s\n", keyptr, arg->query);

์œ„ ๋ถ€๋ถ„์—์„œ ๋งŒ์ผ overflow๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด '\n'์ด ์ž…๋ ฅ๋˜์ง€ ์•Š์•„ ๊ทธ ๋‹ค์Œ ๋ผ์ธ์— ์™€์•ผํ•  ๋ผ์ธ์ด ์ด์–ด ๋ถ™๊ฒŒ๋ ๊ฒƒ ์ž…๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ํ•˜๋Š”๊ฒƒ์€ ์–ด๋–ค๊ฐ€์š”?

    buffer->offset += snprintf(buffer->data + buffer->offset, length - buffer->offset, ...);
    buffer->offset += snprintf(buffer->data + buffer->offset, (length - buffer->offset) > 0 ? (length - buffer->offset) : 0, ...)
        if (buffer->offset >= length) {
        buffer->offset = length - 1;
        buffer->data[buffer->offset - 1] = '\n';
    }

@jhpark816
Copy link
Collaborator

@namsic ์œ„์˜ ์ฝ”๋ฉ˜ํŠธ๋ฅผ ๋ฆฌ๋ทฐํ•ด ์ฃผ์„ธ์š”.

@namsic
Copy link
Collaborator

namsic commented Oct 31, 2024

์œ„ ๋ถ€๋ถ„์—์„œ ๋งŒ์ผ overflow๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด '\n'์ด ์ž…๋ ฅ๋˜์ง€ ์•Š์•„ ๊ทธ ๋‹ค์Œ ๋ผ์ธ์— ์™€์•ผํ•  ๋ผ์ธ์ด ์ด์–ด ๋ถ™๊ฒŒ๋ ๊ฒƒ ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ํ•˜๋Š”๊ฒƒ์€ ์–ด๋–ค๊ฐ€์š”?

keypos์™€ keylen ๊ด€๋ จ ๋ณ€๊ฒฝ์€ ์–ด๋–ป๊ฒŒ ๋˜๋‚˜์š”?

์œ„์˜ ์ฝ”๋ฉ˜ํŠธ๋ฅผ ๋ฆฌ๋ทฐํ•ด ์ฃผ์„ธ์š”.

snprintf์˜ overflow ๊ด€๋ จ ๋ณ€๊ฒฝ์€ ๋…ผ์˜๊ฐ€ ๊ธธ์–ด์งˆ ๊ฒƒ ๊ฐ™์•„ ์ด๋ฒˆ PR์—์„œ ์ œ์™ธํ•˜๊ณ 
๋‹ค์Œ ์ž‘์—…์œผ๋กœ ์ด์–ด์„œ ์ฒ˜๋ฆฌํ–ˆ์œผ๋ฉด ์ข‹๊ฒ ๋Š”๋ฐ, ์ด๋ฒˆ PR์—์„œ ํ•จ๊ป˜ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋‚˜์š”?

@jhpark816
Copy link
Collaborator

snprintf์˜ overflow ๊ด€๋ จ ๋ณ€๊ฒฝ์€ ๋…ผ์˜๊ฐ€ ๊ธธ์–ด์งˆ ๊ฒƒ ๊ฐ™์•„ ์ด๋ฒˆ PR์—์„œ ์ œ์™ธํ•˜๊ณ 
๋‹ค์Œ ์ž‘์—…์œผ๋กœ ์ด์–ด์„œ ์ฒ˜๋ฆฌํ–ˆ์œผ๋ฉด ์ข‹๊ฒ ๋Š”๋ฐ, ์ด๋ฒˆ PR์—์„œ ํ•จ๊ป˜ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋‚˜์š”?

์ด๋ฒˆ PR์—์„œ๋Š” ๋™์ž‘์˜ ์ •ํ™•์„ฑ๋งŒ ๋ณด์žฅ๋˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

@cheesecrust cheesecrust force-pushed the internal/do_lqdetect_write branch from 2b755cb to 198a9da Compare October 31, 2024 04:34
@jhpark816 jhpark816 merged commit 3624d83 into naver:develop Oct 31, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants