-
Notifications
You must be signed in to change notification settings - Fork 67
Add clock attack support. #90
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
6622c21
Add clock
Andrewmatilde 181dae8
Add preProcess
Andrewmatilde 785dd0a
clock attack
Andrewmatilde e4cb49b
impl clock attack
Andrewmatilde 06bb6d0
complete clock attack
Andrewmatilde c8e07a3
complete clock attack
Andrewmatilde 5824744
complete clock attack: add server support
Andrewmatilde 83254c5
minor fix
Andrewmatilde 97fd407
make ExecuteAttack serial & add comment
Andrewmatilde e156d80
minor fix
Andrewmatilde f70654d
minor fix
Andrewmatilde f38e80d
minor fix
Andrewmatilde ebc7815
fix some problem
Andrewmatilde 4c4f8cc
rollback defer
Andrewmatilde 48e9fc0
reorg imports
Andrewmatilde 7518940
fix err handling
Andrewmatilde 96cee6c
minor fix
Andrewmatilde 7f38ed2
bug fix
Andrewmatilde 76b988e
minor fix
Andrewmatilde File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // Copyright 2021 Chaos Mesh Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package attack | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "go.uber.org/fx" | ||
|
|
||
| "github.com/chaos-mesh/chaosd/cmd/server" | ||
| "github.com/chaos-mesh/chaosd/pkg/core" | ||
| "github.com/chaos-mesh/chaosd/pkg/server/chaosd" | ||
| "github.com/chaos-mesh/chaosd/pkg/utils" | ||
| ) | ||
|
|
||
| func NewClockAttackCommand(uid *string) *cobra.Command { | ||
| options := core.NewClockOption() | ||
| dep := fx.Options( | ||
| server.Module, | ||
| fx.Provide(func() *core.ClockOption { | ||
| options.UID = *uid | ||
| return options | ||
| }), | ||
| ) | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "clock attack", | ||
| Short: "clock skew", | ||
| Run: func(*cobra.Command, []string) { | ||
| options.Action = "Attack" | ||
| utils.FxNewAppWithoutLog(dep, fx.Invoke(processClockAttack)).Run() | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().IntVarP(&options.Pid, "pid", "p", 0, "Pid of target program.") | ||
| cmd.Flags().StringVarP(&options.TimeOffset, "time-offset", "t", "", "Specifies the length of time offset.") | ||
| cmd.Flags().StringVarP(&options.ClockIdsSlice, "clock-ids-slice", "c", "CLOCK_REALTIME", | ||
| "The identifier of the particular clock on which to act."+ | ||
| "More clock description in linux kernel can be found in man page of clock_getres, clock_gettime, clock_settime."+ | ||
| "Muti clock ids should be split with \",\"") | ||
| return cmd | ||
| } | ||
|
|
||
| func processClockAttack(options *core.ClockOption, chaos *chaosd.Server) { | ||
| err := options.PreProcess() | ||
| if err != nil { | ||
| utils.ExitWithError(utils.ExitBadArgs, err) | ||
| } | ||
|
|
||
| uid, err := chaos.ExecuteAttack(chaosd.ClockAttack, options, core.CommandMode) | ||
| if err != nil { | ||
| utils.ExitWithError(utils.ExitError, err) | ||
| } | ||
|
|
||
| utils.NormalExit(fmt.Sprintf("Clock attack %v successfully, uid: %s", options, uid)) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| // Copyright 2021 Chaos Mesh Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package core | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
| "syscall" | ||
| "time" | ||
|
|
||
| "github.com/pingcap/log" | ||
| "go.uber.org/zap" | ||
|
|
||
| "github.com/chaos-mesh/chaos-mesh/pkg/time/utils" | ||
| ) | ||
|
|
||
| type ClockOption struct { | ||
| CommonAttackConfig | ||
|
|
||
| Pid int | ||
|
|
||
| TimeOffset string | ||
| SecDelta int64 | ||
| NsecDelta int64 | ||
|
|
||
| ClockIdsSlice string | ||
|
|
||
| Store ClockFuncStore | ||
|
|
||
| ClockIdsMask uint64 | ||
| } | ||
|
|
||
| type ClockFuncStore struct { | ||
| CodeOfGetClockFunc []byte | ||
| OriginAddress uint64 | ||
| } | ||
|
|
||
| func NewClockOption() *ClockOption { | ||
| return &ClockOption{ | ||
| CommonAttackConfig: CommonAttackConfig{ | ||
| Kind: ClockAttack, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (opt *ClockOption) PreProcess() error { | ||
| clkIds := strings.Split(opt.ClockIdsSlice, ",") | ||
|
|
||
| offset, err := time.ParseDuration(opt.TimeOffset) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| opt.SecDelta = int64(offset / time.Second) | ||
| opt.NsecDelta = int64(offset % time.Second) | ||
|
|
||
| clockIdsMask, err := utils.EncodeClkIds(clkIds) | ||
| if err != nil { | ||
| log.Error("error while converting clock ids to mask", zap.Error(err)) | ||
| return err | ||
| } | ||
| if clockIdsMask == 0 { | ||
| log.Error("clock ids must not be empty") | ||
| return fmt.Errorf("clock ids must not be empty") | ||
| } | ||
| opt.ClockIdsMask = clockIdsMask | ||
|
|
||
| if uint64(opt.SecDelta) > 1<<31 { | ||
| log.Warn("Monotonic clock will be broken when sec delta is too large or too small.") | ||
| if uint64(opt.SecDelta) > 1<<56 { | ||
| log.Warn("Time zone info will be broken when sec delta is too large or too small.") | ||
| } | ||
| } | ||
|
|
||
| if uint64(opt.NsecDelta) > 1<<56 { | ||
| log.Warn("Time will be broken when nanosecond delta is too large or too small") | ||
| } | ||
|
|
||
| // Since os.FindProcess in unix systems will always succeed | ||
| // regardless of whether the process exists (https://pkg.go.dev/os#FindProcess), | ||
| // we need to use process.Signal to check if pid is accessible. | ||
| process, err := os.FindProcess(opt.Pid) | ||
| if err != nil { | ||
| log.Error("failed to find process", zap.Error(err)) | ||
| return err | ||
| } | ||
|
|
||
| err = process.Signal(syscall.Signal(0)) | ||
| if err != nil { | ||
| log.Error("pid may not be accessible", zap.Error(err)) | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (opt ClockOption) RecoverData() string { | ||
| data, _ := json.Marshal(opt) | ||
|
|
||
| return string(data) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.